Multithreading and WebSocket
-
I'm trying to connect two or more esp32s that send their sensory information via websocket, but only one is communicating at a time, thus being an asynchronous connection, the opposite of what was supposed to be happening.
Server startup:
WebSocketServer::WebSocketServer(QObject *parent): QObject(parent), websocketserver(new QWebSocketServer(QStringLiteral("ComTrolAcao Server"),QWebSocketServer::NonSecureMode, this)) { if(websocketserver->listen(QHostAddress::Any, 1234)) { qDebug() << "Started server"; connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo); }WebSocket handling:
void WebSocketServer::WebSocketNovo() { QWebSocket *pSocket = websocketserver->nextPendingConnection(); qDebug() << pSocket->peerAddress().toString() << ":" << QString::number(pSocket->peerPort()) << " CONECTADO!\n"; pSocket->setParent(this); connect(pSocket, &QWebSocket::textMessageReceived, this, &WebSocketServer::WebSocketProcessaTexto); connect(pSocket, &QWebSocket::disconnected, this, &WebSocketServer::WebSocketDesconecta); clients << pSocket; qDebug() << clients; } void WebSocketServer::WebSocketProcessaTexto(QString mensagem) { QStringList dados; QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); //qDebug() << "MENSAGE RECEVEID:" << mensagem; } void WebSocketServer::WebSocketDesconecta() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { clients.removeAll(pClient); pClient->deleteLater(); } }As a solution I thought of using threads to solve this, opening a thread for each connection so that they can communicate all at once through the websocket. I know it's wrong, but something like:
if(websocketserver->listen(QHostAddress::Any, 1234)) { qDebug() << "Started Server"; QtConcurrent::run(connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo)); }How can I use QTConcurrent to open independent threads for each Websocket connection or how can I use threads for this without QTConcurrent?
Thanks, Lima.
-
I'm trying to connect two or more esp32s that send their sensory information via websocket, but only one is communicating at a time, thus being an asynchronous connection, the opposite of what was supposed to be happening.
Server startup:
WebSocketServer::WebSocketServer(QObject *parent): QObject(parent), websocketserver(new QWebSocketServer(QStringLiteral("ComTrolAcao Server"),QWebSocketServer::NonSecureMode, this)) { if(websocketserver->listen(QHostAddress::Any, 1234)) { qDebug() << "Started server"; connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo); }WebSocket handling:
void WebSocketServer::WebSocketNovo() { QWebSocket *pSocket = websocketserver->nextPendingConnection(); qDebug() << pSocket->peerAddress().toString() << ":" << QString::number(pSocket->peerPort()) << " CONECTADO!\n"; pSocket->setParent(this); connect(pSocket, &QWebSocket::textMessageReceived, this, &WebSocketServer::WebSocketProcessaTexto); connect(pSocket, &QWebSocket::disconnected, this, &WebSocketServer::WebSocketDesconecta); clients << pSocket; qDebug() << clients; } void WebSocketServer::WebSocketProcessaTexto(QString mensagem) { QStringList dados; QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); //qDebug() << "MENSAGE RECEVEID:" << mensagem; } void WebSocketServer::WebSocketDesconecta() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { clients.removeAll(pClient); pClient->deleteLater(); } }As a solution I thought of using threads to solve this, opening a thread for each connection so that they can communicate all at once through the websocket. I know it's wrong, but something like:
if(websocketserver->listen(QHostAddress::Any, 1234)) { qDebug() << "Started Server"; QtConcurrent::run(connect(websocketserver, &QWebSocketServer::newConnection, this, &WebSocketServer::WebSocketNovo)); }How can I use QTConcurrent to open independent threads for each Websocket connection or how can I use threads for this without QTConcurrent?
Thanks, Lima.
@tux_brazilian said in Multithreading and WebSocket:
but only one is communicating at a time,
What does this mean? Does the second one not connect to your WebSocket? Or do you remove the second connection or what exactly does not work?
No need for a thread here - will not help at all. -
@Christian-Ehrlicher said in Multithreading and WebSocket:
What does this mean? Does the second one not connect to your WebSocket? Or do you remove the second connection or what exactly does not work?
As the esp32 is constantly sending information from the sensors, the first one connects and starts transmitting this data via websocket, but the second esp32 even connects to the server, but since the first one is already transmitting, the second one cannot transmit, in case I disconnect the second esp32, will soon start transmitting through the websocket
-
@Christian-Ehrlicher said in Multithreading and WebSocket:
What does this mean? Does the second one not connect to your WebSocket? Or do you remove the second connection or what exactly does not work?
As the esp32 is constantly sending information from the sensors, the first one connects and starts transmitting this data via websocket, but the second esp32 even connects to the server, but since the first one is already transmitting, the second one cannot transmit, in case I disconnect the second esp32, will soon start transmitting through the websocket
@tux_brazilian said in Multithreading and WebSocket:
to the server, but since the first one is already transmitting, the second one cannot transmit,
Why should this not be possible? A server can handle more than one client connection and your code you show us already does it correctly
-
Yes, but when you tried the clients at the same time, the server couldn't handle it and disconnected
-
Yes, but when you tried the clients at the same time, the server couldn't handle it and disconnected
@tux_brazilian said in Multithreading and WebSocket:
the server couldn't handle it and disconnected
Since you program the server I would guess you're doing something wrong. The server does not disconnect a connection by itself. You must be doing it somehow.