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-&
gt;listen(QHostAddress::
Any, port)) {
if
(m_debug)
qDebug() &
lt;&
lt; "Echoserver listening on port"
&
lt;&
lt; port;
connect(m_pWebSocketServer, &
amp;QWebSocketServer::
newConnection,
this
, &
amp;EchoServer::
onNewConnection);
connect(m_pWebSocketServer, &
amp;QWebSocketServer::
closed, this
, &
amp;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-&
gt;nextPendingConnection();
connect(pSocket, &
amp;QWebSocket::
textMessageReceived, this
, &
amp;EchoServer::
processTextMessage);
connect(pSocket, &
amp;QWebSocket::
binaryMessageReceived, this
, &
amp;EchoServer::
processBinaryMessage);
connect(pSocket, &
amp;QWebSocket::
disconnected, this
, &
amp;EchoServer::
socketDisconnected);
m_clients &
lt;&
lt; 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&
lt;QWebSocket *&
gt;(sender());
if
(m_debug)
qDebug() &
lt;&
lt; "Message received:"
&
lt;&
lt; message;
if
(pClient) {
pClient-&
gt;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&
lt;QWebSocket *&
gt;(sender());
if
(m_debug)
qDebug() &
lt;&
lt; "Binary Message received:"
&
lt;&
lt; message;
if
(pClient) {
pClient-&
gt;sendBinaryMessage(message);
}
}
The only difference is that the message now is a QByteArray instead of a QString.
void
EchoServer::
socketDisconnected()
{
QWebSocket *
pClient =
qobject_cast&
lt;QWebSocket *&
gt;(sender());
if
(m_debug)
qDebug() &
lt;&
lt; "socketDisconnected:"
&
lt;&
lt; pClient;
if
(pClient) {
m_clients.removeAll(pClient);
pClient-&
gt;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.