背景
近期负责一个项目,需要用到socket.io c++, 我所使用的库是socket.io-client-cpp https://github.com/socketio/socket.io-client-cpp.git,qt环境,用的是Without Cmake的方式。
问题
在与服务端进行连接时,报下面的错误:
连接服务端代码:
QString loginUrl="https://xxxxxx/login";
map<string,string> query;
query.insert(make_pair("token",token));
query.insert(make_pair("client","web"));
h.connect(loginUrl.toStdString(),query);
返回错误:
[2021-09-18 09:37:20] [connect] Successful connection
[2021-09-18 09:37:20] [error] Server handshake response error: websocketpp.processor:20 (Invalid HTTP status.)
解决方案
- 在.pro里面添加 SIO_TLS
DEFINES += SIO_TLS
- 添加 openssl 库,把相关.dll文件移入项目releas | debug 文件夹中
INCLUDEPATH += D:\install\openssl\OpenSSL-Win64\include
LIBS += D:\install\openssl\OpenSSL-Win64\lib\openssl.lib
LIBS += D:\install\openssl\OpenSSL-Win64\lib\libcrypto.lib
LIBS += D:\install\openssl\OpenSSL-Win64\lib\libssl.lib
- 在 sio_client_impl.cpp 中取消注释 #include <asio/ssl/impl/src.hpp>
#if SIO_TLS
// If using Asio's SSL support, you will also need to add this #include.
// Source: http://think-async.com/Asio/asio-1.10.6/doc/asio/using.html
#include <asio/ssl/impl/src.hpp>
#endif
- 运行会报错,找到相关位置,直接注释掉
报错:
# error Do not compile Asio library source with ASIO_HEADER_ONLY defined
//#if defined(ASIO_HEADER_ONLY)
//# error Do not compile Asio library source with ASIO_HEADER_ONLY defined
//#endif
成功
[2021-09-18 10:10:04] [connect] Successful connection
[2021-09-18 10:10:04] [connect] WebSocket Connection xxx.xxx.xxx.xxx:443 v-2 "WebSocket++/0.8.2" /socket.io/?EIO=3&transport=websocket&t=1631931004&client=web&token=74f1a120%2D0d50%2D11ec%2Db642%2Dcd1d0d4176f5 101
结语
如有错误,请指正
本文介绍了一种使用socket.io-client-cpp库时遇到的连接错误及其解决方案,通过配置TLS和openssl库,最终成功解决了WebSocket连接问题。

257

被折叠的 条评论
为什么被折叠?



