Skip to content

支持异步IO(io_uring)与websocket的C++web服务器

Notifications You must be signed in to change notification settings

799034552/new_cpp_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web c++服务器

linux c++服务器,由于c++语法本身繁琐,大多数的cpp服务器都不能像node服务器一样能快速上手,因此,打算做一个仿nodejs的express框架风格的c++服务器,懂一点点c++就能快速上手部署

亮点

✅大文件使用异步IO(io_uring),防止阻塞

✅支持websocket服务器推送

✅仿nodejs的express框架语法,上手简单

✅支持get、post,以及静态文件

快速编译

基于cmake,需要先安装liburing(异步io封装)与openssl:sudo apt-get install libssl-dev

cmake -B build
cmake --build build
./build/test_cpp_server

快速使用

GET请求

#include<cpp_server/Server.h>
#include<stdio.h>
int main(){
    Server app;
    // 监听端口
    app.listen(8000, []{
        printf("监听在8000端口\n");
    }, 4);
    
    // get请求
    app.get("/", [](Req &req, Res &res){
        printf("name: %s\n", req.kv_data.at("name").c_str()); // 获取get参数
        res.send("hello get");
    });
    app.run();
}

POST请求

app.post("/", [](Req &req, Res &res){
    printf("name: %s\n", req.kv_data.at("name").c_str()); // 获取post参数
    res.send("hello post");
});

websocket请求

app.ws("/websocket", [](WSClient* client){
    client->on_connect([](WSClient* _client){
        _client->send("hello socket");
    });
    client->on_get([](string data,WSClient* _client){
        _client->send("i get " + data);
    });
});

静态文件映射

app.static_file("/static", "/var/www/static");
// 访问127.0.0.1/static/xxx 就是访问 /var/www/static/xxx

整体框架

img

webbench压力测试

在没有下载文件时

img

正在外网下载一个很大的文件时

img

img

参考

About

支持异步IO(io_uring)与websocket的C++web服务器

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published