diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/echoclient/echoclient.cpp | 1 | ||||
-rw-r--r-- | examples/echoclient/echoclient.h | 1 | ||||
-rw-r--r-- | examples/echoclient/main.cpp | 1 | ||||
-rw-r--r-- | examples/echoserver/echoserver.cpp | 28 | ||||
-rw-r--r-- | examples/echoserver/echoserver.h | 2 | ||||
-rw-r--r-- | examples/echoserver/main.cpp | 5 |
6 files changed, 24 insertions, 14 deletions
diff --git a/examples/echoclient/echoclient.cpp b/examples/echoclient/echoclient.cpp index 64a95ea..452b20e 100644 --- a/examples/echoclient/echoclient.cpp +++ b/examples/echoclient/echoclient.cpp @@ -50,6 +50,7 @@ EchoClient::EchoClient(const QUrl &url, QObject *parent) : m_url(url) { connect(&m_webSocket, SIGNAL(connected()), this, SLOT(onConnected())); + connect(&m_webSocket, SIGNAL(disconnected()), this, SIGNAL(closed())); m_webSocket.open(QUrl(url)); } //! [constructor] diff --git a/examples/echoclient/echoclient.h b/examples/echoclient/echoclient.h index dd71497..497d7c5 100644 --- a/examples/echoclient/echoclient.h +++ b/examples/echoclient/echoclient.h @@ -53,6 +53,7 @@ public: explicit EchoClient(const QUrl &url, QObject *parent = Q_NULLPTR); Q_SIGNALS: + void closed(); public Q_SLOTS: diff --git a/examples/echoclient/main.cpp b/examples/echoclient/main.cpp index d93fd24..a13097d 100644 --- a/examples/echoclient/main.cpp +++ b/examples/echoclient/main.cpp @@ -45,6 +45,7 @@ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); EchoClient client(QUrl("ws://localhost:1234")); + QObject::connect(&client, SIGNAL(closed()), &a, SLOT(quit())); Q_UNUSED(client); diff --git a/examples/echoserver/echoserver.cpp b/examples/echoserver/echoserver.cpp index 0825f5d..8a2676c 100644 --- a/examples/echoserver/echoserver.cpp +++ b/examples/echoserver/echoserver.cpp @@ -48,18 +48,27 @@ QT_USE_NAMESPACE //! [constructor] EchoServer::EchoServer(quint16 port, QObject *parent) : QObject(parent), - m_pWebSocketServer(Q_NULLPTR), + m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"), + QWebSocketServer::NON_SECURE_MODE, this)), m_clients() { - m_pWebSocketServer = new QWebSocketServer("Echo Server", QWebSocketServer::NON_SECURE_MODE, this); - if (m_pWebSocketServer->listen(QHostAddress::Any, port)) - { + if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { qDebug() << "Echoserver listening on port" << port; connect(m_pWebSocketServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); + connect(m_pWebSocketServer, SIGNAL(closed()), this, SIGNAL(closed())); } } //! [constructor] +EchoServer::~EchoServer() +{ + m_pWebSocketServer->close(); + while (!m_clients.isEmpty()) { + QWebSocket *pWebSocket = m_clients.takeFirst(); + delete pWebSocket; + } +} + //! [onNewConnection] void EchoServer::onNewConnection() { @@ -68,7 +77,6 @@ void EchoServer::onNewConnection() connect(pSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(processMessage(QString))); connect(pSocket, SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(processBinaryMessage(QByteArray))); connect(pSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); - //connect(pSocket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); m_clients << pSocket; } @@ -78,10 +86,10 @@ void EchoServer::onNewConnection() void EchoServer::processMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); - if (pClient) - { + if (pClient) { pClient->write(message); } + m_pWebSocketServer->close(); } //! [processMessage] @@ -89,8 +97,7 @@ void EchoServer::processMessage(QString message) void EchoServer::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); - if (pClient) - { + if (pClient) { pClient->write(message); } } @@ -100,8 +107,7 @@ void EchoServer::processBinaryMessage(QByteArray message) void EchoServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); - if (pClient) - { + if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } diff --git a/examples/echoserver/echoserver.h b/examples/echoserver/echoserver.h index 0333f8f..2cd8922 100644 --- a/examples/echoserver/echoserver.h +++ b/examples/echoserver/echoserver.h @@ -53,8 +53,10 @@ class EchoServer : public QObject Q_OBJECT public: explicit EchoServer(quint16 port, QObject *parent = Q_NULLPTR); + ~EchoServer(); Q_SIGNALS: + void closed(); private Q_SLOTS: void onNewConnection(); diff --git a/examples/echoserver/main.cpp b/examples/echoserver/main.cpp index b424666..def405f 100644 --- a/examples/echoserver/main.cpp +++ b/examples/echoserver/main.cpp @@ -44,9 +44,8 @@ int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); - EchoServer server(1234); - - Q_UNUSED(server); + EchoServer *server = new EchoServer(1234); + QObject::connect(server, SIGNAL(closed()), &a, SLOT(quit())); return a.exec(); } |