WebSocket server example
Description
The echoserver example implements a WebSocket server that echoes back everything that is sent to it.
Code
We start by creating a QWebSocketServer (`new QWebSocketServer()`). After the creation, we listen on all local network interfaces (`QHostAddress::Any`) on the specified port.
EchoServer::EchoServer(quint16 port, QObject *parent) : QObject(parent), m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"), QWebSocketServer::NonSecureMode, this)), m_clients() { if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { qDebug() << "Echoserver listening on port" << port; connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &EchoServer::onNewConnection); connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &EchoServer::closed); } }
If listening is successful, we connect the `newConnection()` signal to the slot `onNewConnection()`. The `newConnection()` signal will be thrown whenever a new WebSocket client is connected to our server.
void EchoServer::onNewConnection() { QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); connect(pSocket, &QWebSocket::textMessageReceived, this, &EchoServer::processTextMessage); connect(pSocket, &QWebSocket::binaryMessageReceived, this, &EchoServer::processBinaryMessage); connect(pSocket, &QWebSocket::disconnected, this, &EchoServer::socketDisconnected); m_clients << pSocket; }
When a new connection is received, the client QWebSocket is retrieved (`nextPendingConnection()`), and the signals we are interested in are connected to our slots (`textMessageReceived()`, `binaryMessageReceived()` and `disconnected()`). The client socket is remembered in a list, in case we would like to use it later (in this example, nothing is done with it).
void EchoServer::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { pClient->sendTextMessage(message); } m_pWebSocketServer->close(); }
Whenever `processTextMessage()` is triggered, we retrieve the sender, and if valid, send back the original message (`sendTextMessage()`). The same is done with binary messages.
void EchoServer::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { pClient->sendBinaryMessage(message); } }
The only difference is that the message now is a QByteArray instead of a QString.
void EchoServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } }
Whenever a socket is disconnected, we remove it from the clients list and delete the socket. Note: it is best to use `deleteLater()` to delete the socket.
© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.