WebSocket server example | Qt WebSockets 5.15.18

- Qt 5.15
- Qt WebSockets
- WebSocket server example
- Qt 5.15.18 Reference Documentation

# 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, bool debug, QObject *parent) :
         QObject(parent),
         m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"),
                                                 QWebSocketServer::NonSecureMode, this)),
         m_debug(debug)
     {
         if (m_pWebSocketServer->listen(QHostAddress::Any, port)) {
             if (m_debug)
                 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 (m_debug)
             qDebug() << "Message received:" << message;
         if (pClient) {
             pClient->sendTextMessage(message);
         }
     }

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 (m_debug)
             qDebug() << "Binary Message received:" << message;
         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 (m_debug)
             qDebug() << "socketDisconnected:" << pClient;
         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.