uWebSockets的使用(二):uWebSockets的编译和使用

参考网址:

uWebSockets项目地址:https://github.com/uNetworking/uWebSockets

uSockets项目地址:https://github.com/uNetworking/uSockets

一、摘要:

1、编译和使用uWebSockets的环境为win10,vs2017 专业版,所有的依赖库都是32位debug版本的,Linux上未尝试

2、uWebSockets编译所需依赖库为:

<1> libuv:请参考我之前的博文,将此库编译为静态库:C网络库使用(一):libuv的安装、多线程tcp客户端、多线程tcp服务器的使用_wangdamingll的博客-CSDN博客

<2> uSockets:请参考我之前的博文,将此库编译为静态库:uWebSockets的使用(一):uSockets的编译和使用(一)_wangdamingll的博客-CSDN博客_uwebsockets

<3> zlib:这里不做如何编译成库,官网网址:zlib Home Site 

二、构建vs2017工程

因为uWebSockets除uSocekts之外,是使用C++17标准的头文件构建的,不需要编译,直接包含uWebSockets项目地址下的src头文件即可。

一下是我的common库文件结构:

common包含4个文件夹:

libuv: 包含libuv库的头文件和静态库

uSockets:包含uSocekts的头文件和静态库

zlib:包含zlib的头文件和静态库

uWebSockets:直接包含uWebSockets项目地址下的src下的所有文件

1、新建vs2017空项目

<1>项目设置


1)VC++ 目录->包含目录, 将common所有文件夹中的头文件包含进来

2)VC++ 目录->库目录, 将common所有文件夹的静态库路径包含进来

3)C/C++ ->预处理器,添加UWS_NO_ZLIB,去除zlib功能,我这里将zlib的静态库接入工程始终不成功,所以直接禁用zlib功能

4)C/C++ ->语言,符合模式改为否,否则编译报错

5)C/C++ ->语言,C++ 语言标准改为 ISO C++17 标准 (/std:c++17)

6)链接器 ->输入->附加依赖项,添加 zlib.lib
libuv.lib
uSockets_32.lib
userenv.lib
Iphlpapi.lib
psapi.lib

7)链接器 ->输入->忽略特定默认库,添加 LIBCMTD

<2> 在项目中添加源文件


1)右击头文件->添加现有项,将common库下uWebSockets下的所有文件添加进工程中

2)右击源文件->添加现有项,将uWebSockets项目地址下examples下的EchoServer.cpp添加进工程中

<3>编译工程

你没有看错哦,有错误,如果你没有这个错误,那么非常恭喜你。至少我这里是不成功的。这个问题我查了很久,从uWebSockets项目地址下的issue中发现了蛛丝马迹。所以我在这里也只能猜测(毕竟官方的例子别人都能运行):

uWS::App().ws<PerSocketData>("/*", {.....})

这里{.....}中的内容应该是C++17标准中的语法,是给App.h中此结构体成员变量赋值的:

    struct WebSocketBehavior {
        CompressOptions compression = DISABLED;
        int maxPayloadLength = 16 * 1024;
        int idleTimeout = 120;
        int maxBackpressure = 1 * 1024 * 1204;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
    };

然而非常抱歉的是,我这vs 2017专业版对此语法并不支持,虽然我vs 2017语言标准选择的是C++ 17标准。

<4>修改源码改变上述赋值方式

在这里我只修改了App.h中的部分源码:

原来命名空间下增加WebSocketBehavior模板,并将struct TemplatedApp {...}类下WebSocketBehavior注释掉,

接着修改struct TemplatedApp {...}类下ws的第二个参数,源码修改完成. 以下为示例:

namespace uWS {

...

template <bool SSL>
struct WebSocketBehavior {
    CompressOptions compression = DISABLED;
    int maxPayloadLength = 16 * 1024;
    int idleTimeout = 120;
    int maxBackpressure = 1 * 1024 * 1204;
    fu2::unique_function<void(uWS::WebSocket<false, true> *, HttpRequest *)> open = nullptr;
    fu2::unique_function<void(uWS::WebSocket<false, true> *, std::string_view, uWS::OpCode)> message = nullptr;
    fu2::unique_function<void(uWS::WebSocket<false, true> *)> drain = nullptr;
    fu2::unique_function<void(uWS::WebSocket<false, true> *)> ping = nullptr;
    fu2::unique_function<void(uWS::WebSocket<false, true> *)> pong = nullptr;
    fu2::unique_function<void(uWS::WebSocket<false, true> *, int, std::string_view)> close = nullptr;
};
typedef WebSocketBehavior<false> Behavior;
typedef WebSocketBehavior<true> SSLBehavior;

...

template <bool SSL>
struct TemplatedApp {

...

    /*
    struct WebSocketBehavior {
        CompressOptions compression = DISABLED;
        int maxPayloadLength = 16 * 1024;
        int idleTimeout = 120;
        int maxBackpressure = 1 * 1024 * 1204;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
        fu2::unique_function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
    };
    */

...

    template <typename UserData>
    //TemplatedApp &&ws(std::string pattern, WebSocketBehavior &&behavior) {
    TemplatedApp &&ws(std::string pattern, WebSocketBehavior<SSL> &&behavior) {

...

}

...

}

}

<5>修改官方给的例子

这是我上传到github上的网址: https://github.com/wangdamingll/uWebSocektsDemo.git


int main() {
    /* ws->getUserData returns one of these */
    struct PerSocketData {

    };
    
    auto app = uWS::App();
    auto behavior = uWS::Behavior();
    behavior.compression= uWS::DISABLED;
    behavior.maxPayloadLength = 16 * 1024;
    behavior.idleTimeout = 10;
    behavior.open = [](auto *ws, auto *req) {

    };
    behavior.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
        std::cout << "message: " << message << std::endl;
        ws->send(message, opCode);
    };
    behavior.drain = [](auto *ws) {

    };
    behavior.ping = [](auto *ws) {

    };
    behavior.pong = [](auto *ws) {

    };
    behavior.close = [](auto *ws, int code, std::string_view message) {

    };

    app.ws<PerSocketData>("/*", (uWS::Behavior&&)behavior).listen(9001, [](auto *token) {
            if (token) {
                std::cout << "Listening on port " << 9001 << std::endl;
            }
    }).run();

}

<6>重新编译运行例子

2、使用总结

个人总结一下,此库不太好用,编译比较麻烦,官方给的demo使用信息较少

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值