qt 构建websocket多线程服务器

在工作中总有特殊的需求 比如用客户端框架qt构建websocket服务器

使用QWebsocketserver监听 nextPendingConnection得到的websocket无法跨线程使用

在QTcpServer中我们可以重写inComming函数得到socket描述符传入多线程中再使用QTcpSocket的setSocketDescript函数解决问题 然而QWebSocketServer并没有此函数。

所以使用QWebSocketServer的handleConnection升级QTcpSocket 但handleConnection返回值为void 如何获得转换后的websocket?查看qt源码在这里插入图片描述

再追踪handshakeReceived得

在这里插入图片描述

addPendingConnection为添加构建的websocket到 nextPendingConnection拿取的队列 可以看到添加队列以后还会发出newConnection()信号

所以只要再handleConnection升级QTcpSocket语句之后 链接调用此函数的QWebSocketServer对象的newConnections信号 再通过nextPendingConnection函数拿到

Qt中使用QtWebSockets库进行多线程WebSocket通信是很常见的需求。以下是一个简单的示例,展示了如何在多个线程中使用QtWebSockets。 首先,确保已经添加了QtWebSockets模块到你的Qt项目中。 ```cpp #include <QtCore> #include <QtWebSockets> class WebSocketClient : public QObject { Q_OBJECT public: explicit WebSocketClient(QObject *parent = nullptr) : QObject(parent) { } public slots: void connectToServer(const QUrl &url) { QWebSocket *socket = new QWebSocket(); connect(socket, &QWebSocket::connected, this, &WebSocketClient::onConnected); connect(socket, &QWebSocket::disconnected, this, &WebSocketClient::onDisconnected); connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebSocketClient::onError); socket->open(url); } signals: void connected(); void disconnected(); void error(const QString &message); private slots: void onConnected() { QWebSocket *socket = qobject_cast<QWebSocket *>(sender()); if (socket) { connect(socket, &QWebSocket::textMessageReceived, this, &WebSocketClient::onTextMessageReceived); emit connected(); } } void onDisconnected() { QWebSocket *socket = qobject_cast<QWebSocket *>(sender()); if (socket) { socket->deleteLater(); emit disconnected(); } } void onError(QAbstractSocket::SocketError error) { QWebSocket *socket = qobject_cast<QWebSocket *>(sender()); if (socket) { emit error(socket->errorString()); } } void onTextMessageReceived(const QString &message) { QWebSocket *socket = qobject_cast<QWebSocket *>(sender()); if (socket) { // 处理接收到的消息 qDebug() << "Received message:" << message; } } }; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QThread workerThread; WebSocketClient webSocketClient; webSocketClient.moveToThread(&workerThread); workerThread.start(); QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() { workerThread.quit(); workerThread.wait(); }); QUrl url("wss://your-websocket-url.com"); QMetaObject::invokeMethod(&webSocketClient, "connectToServer", Qt::QueuedConnection, Q_ARG(QUrl, url)); return app.exec(); } #include "main.moc" ``` 在上面的示例中,我们创建了一个`WebSocketClient`类,用于管理WebSocket连接。该类继承自`QObject`,并包含了与服务器连接、断开连接以及接收消息相关的槽函数和信号。 在`main()`函数中,我们创建了一个`QThread`对象和一个`WebSocketClient`对象,并将`WebSocketClient`对象移动到新创建的工作线程中。然后,我们通过调用`QMetaObject::invokeMethod()`方法在工作线程中异步地调用`connectToServer()`槽函数来连接到服务器。 请注意,QtWebSockets库本身是异步的,因此不需要显式地创建额外的线程来处理WebSocket通信。将`WebSocketClient`对象移动到线程中,可以确保WebSocket操作在独立的线程中执行,而不会阻塞主线程。 这只是一个简单的示例,你可以根据自己的需求进行更复杂的处理。希望对你有所帮助!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值