QWebSocketServer does not accept new connections
-
Here my own implementation of a wrapper for a web socket communication:
#include "wsserver.h" #include <QDebug> #define DEBUG const QString MODULE_ID = "[WSS]"; WsServer::WsServer(QObject *parent) : QObject(parent) { } WsServer::~WsServer() { qInfo() << MODULE_ID << "Closing connections"; _wsServer->close(); } void WsServer::listen(quint16 port) { _wsServer = new QWebSocketServer("websocket", QWebSocketServer::NonSecureMode, this); if (_wsServer->listen(QHostAddress::Any, port)) { qInfo() << MODULE_ID << "WebSocket server is listening on port" << port; connect(_wsServer, &QWebSocketServer::newConnection, this, &WsServer::newConnection); connect(_wsServer, &QWebSocketServer::closed, this, &WsServer::closed); } } void WsServer::sendTextMessage(QString message) { foreach (QWebSocket *client, _listClients) { client->sendTextMessage(message); } } void WsServer::newConnection() { QWebSocket *socket = _wsServer->nextPendingConnection(); connect(socket, &QWebSocket::textMessageReceived, this, &WsServer::processTextMessage); connect(socket, &QWebSocket::binaryMessageReceived, this, &WsServer::processBinaryMessage); connect(socket, &QWebSocket::disconnected, this, &WsServer::socketDisconnected); _listClients.append(socket); emit clientConnected(); #ifdef DEBUG qInfo() << MODULE_ID << "New client connected" << socket << socket->peerAddress().toString() << QString("(%1)").arg(_listClients.size()); #endif } void WsServer::socketDisconnected() { QWebSocket *socket = qobject_cast<QWebSocket *>(sender()); #ifdef DEBUG qInfo() << MODULE_ID << "Socket disconnected" << socket; #endif if (socket) { _listClients.removeAll(socket); socket->deleteLater(); } } void WsServer::processTextMessage(QString message) { #ifdef DEBUG qInfo() << MODULE_ID << "Text message received:" << message; #endif emit textMessageReceived(message); } void WsServer::processBinaryMessage(QByteArray message) { #ifdef DEBUG qInfo() << MODULE_ID << "Binary message received:" << message; #endif emit binaryMessageReceived(message); }
It works fine and every time a client connects I get the related debug message.
When the browser changes (or reloads) the page I get the socket disconnected and just after the socket connected message.As you can see, when a socket connects I print the number of clients currently connected:
qInfo() << MODULE_ID << "New client connected" << socket << socket->peerAddress().toString() << QString("(%1)").arg(_listClients.size());
and it's always 1, since I'm doing my tests with one browser only.
After changing/reloading the page some times the javascript websocket is not able to connect anymore.In this situation if I close the application I get a long list of messages about new connections and disconnections. I guess there are the failed connections I've described above.
What's wrong here? What should I change in order to keep clean the socket list and allow the browser to always connect?
-
Hi,
One thing that is missing is the error handling. You should add that to see if something else is going on.
-
@SGaist I catch the following signals:
WebSocket::error
QWebSocketServer::acceptError
QWebSocketServer::acceptError
and I was able to detect a
CloseCodeBadOperation
only when I refreshed the browser page very fast, nothing else.This morning I launched my application and I connected a device to the websocket. Got the notification. Then I closed the browser. Got the notification.
Nothing else happened, but after few hours I tried again to connect but the javascript code timed out like above. No errors server side.
When I closed the application I got all the pending connection/disconnection notifications.
Please note that the browser and the server are on the same machine (I'm connecting as
localhost
).Any hint would be very appreciated!
-
Are you using multiple threads ?