summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2017-09-25 14:16:58 (GMT)
committerMårten Nordheim <marten.nordheim@qt.io>2017-09-28 13:21:27 (GMT)
commit71b5fc5dd10e9edce8db886f6c05b7950b3cca6e (patch)
treeff3b1a8543aae7abd3d1270eb7336c662bdf5c5c
parentfdb6d8370f9ae8fa148124289317c108399853c3 (diff)
downloadqtwebsockets-71b5fc5dd10e9edce8db886f6c05b7950b3cca6e.tar.gz
Revamp WebSocket's simple-chat examplev5.10.0-beta1
- Replace Q_* macros with their 'modern' substitutes - Replace a usage of qDebug with QTextStream - Print a little piece of text whenever a client connects - Add a textbox to the html page to set the host to connect to - Enable/disable disconnect and send button based on connection Task-number: QTBUG-60656 Change-Id: Ieec571c3964f94cd464912054acda3208c02c898 Reviewed-by: Liang Qi <liang.qi@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
-rw-r--r--examples/websockets/simplechat/chatclient.html15
-rw-r--r--examples/websockets/simplechat/chatserver.cpp40
-rw-r--r--examples/websockets/simplechat/chatserver.h8
-rw-r--r--examples/websockets/simplechat/main.cpp1
4 files changed, 41 insertions, 23 deletions
diff --git a/examples/websockets/simplechat/chatclient.html b/examples/websockets/simplechat/chatclient.html
index 511d05b..d2dbf47 100644
--- a/examples/websockets/simplechat/chatclient.html
+++ b/examples/websockets/simplechat/chatclient.html
@@ -5,17 +5,20 @@
<body>
<h1>WebSocket Chat Client</h1>
<p>
+ Host: <input id="webSocketHost" type="text" value="localhost:1234"/>
+ </p>
+ <p>
<button onClick="initWebSocket();">Connect</button>
- <button onClick="stopWebSocket();">Disconnect</button>
+ <button id="disconnectButton" onClick="stopWebSocket();" disabled>Disconnect</button>
<button onClick="checkSocket();">State</button>
</p>
<p>
- <textarea id="debugTextArea" style="width:400px;height:200px;"></textarea>
+ <textarea id="debugTextArea" style="width:400px;height:200px;" readonly></textarea>
</p>
<p>
<input type="text" id="inputNick" value="nickname" />
<input type="text" id="inputText" onkeydown="if(event.keyCode==13)sendMessage();"/>
- <button onClick="sendMessage();">Send</button>
+ <button id="sendButton" onClick="sendMessage();" disabled>Send</button>
</p>
<script type="text/javascript">
@@ -38,7 +41,6 @@
}
}
- var wsUri = "ws://localhost:1234";
var websocket = null;
function initWebSocket() {
@@ -47,12 +49,17 @@
WebSocket = MozWebSocket;
if ( websocket && websocket.readyState == 1 )
websocket.close();
+ var wsUri = "ws://" + document.getElementById("webSocketHost").value;
websocket = new WebSocket( wsUri );
websocket.onopen = function (evt) {
debug("CONNECTED");
+ document.getElementById("disconnectButton").disabled = false;
+ document.getElementById("sendButton").disabled = false;
};
websocket.onclose = function (evt) {
debug("DISCONNECTED");
+ document.getElementById("disconnectButton").disabled = true;
+ document.getElementById("sendButton").disabled = true;
};
websocket.onmessage = function (evt) {
console.log( "Message received :", evt.data );
diff --git a/examples/websockets/simplechat/chatserver.cpp b/examples/websockets/simplechat/chatserver.cpp
index ae207f5..8885fe8 100644
--- a/examples/websockets/simplechat/chatserver.cpp
+++ b/examples/websockets/simplechat/chatserver.cpp
@@ -48,23 +48,31 @@
**
****************************************************************************/
#include "chatserver.h"
-#include "QtWebSockets/QWebSocketServer"
-#include "QtWebSockets/QWebSocket"
-#include <QtCore/QDebug>
+
+#include <QtWebSockets>
+#include <QtCore>
+
+#include <cstdio>
+using namespace std;
QT_USE_NAMESPACE
+static QString getIdentifier(QWebSocket *peer)
+{
+ return QStringLiteral("%1:%2").arg(peer->peerAddress().toString(),
+ QString::number(peer->peerPort()));
+}
+
//! [constructor]
ChatServer::ChatServer(quint16 port, QObject *parent) :
QObject(parent),
- m_pWebSocketServer(Q_NULLPTR)
+ m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Chat Server"),
+ QWebSocketServer::NonSecureMode,
+ this))
{
- m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Chat Server"),
- QWebSocketServer::NonSecureMode,
- this);
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
- qDebug() << "Chat Server listening on port" << port;
+ QTextStream(stdout) << "Chat Server listening on port " << port << '\n';
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &ChatServer::onNewConnection);
}
@@ -73,31 +81,32 @@ ChatServer::ChatServer(quint16 port, QObject *parent) :
ChatServer::~ChatServer()
{
m_pWebSocketServer->close();
- qDeleteAll(m_clients.begin(), m_clients.end());
}
//! [constructor]
//! [onNewConnection]
void ChatServer::onNewConnection()
{
- QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
+ auto pSocket = m_pWebSocketServer->nextPendingConnection();
+ QTextStream(stdout) << getIdentifier(pSocket) << " connected!\n";
+ pSocket->setParent(this);
- connect(pSocket, &QWebSocket::textMessageReceived, this, &ChatServer::processMessage);
- connect(pSocket, &QWebSocket::disconnected, this, &ChatServer::socketDisconnected);
+ connect(pSocket, &QWebSocket::textMessageReceived,
+ this, &ChatServer::processMessage);
+ connect(pSocket, &QWebSocket::disconnected,
+ this, &ChatServer::socketDisconnected);
m_clients << pSocket;
}
//! [onNewConnection]
//! [processMessage]
-void ChatServer::processMessage(QString message)
+void ChatServer::processMessage(const QString &message)
{
QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());
for (QWebSocket *pClient : qAsConst(m_clients)) {
if (pClient != pSender) //don't echo message back to sender
- {
pClient->sendTextMessage(message);
- }
}
}
//! [processMessage]
@@ -106,6 +115,7 @@ void ChatServer::processMessage(QString message)
void ChatServer::socketDisconnected()
{
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
+ QTextStream(stdout) << getIdentifier(pClient) << " disconnected!\n";
if (pClient)
{
m_clients.removeAll(pClient);
diff --git a/examples/websockets/simplechat/chatserver.h b/examples/websockets/simplechat/chatserver.h
index 4c652a3..4a8285b 100644
--- a/examples/websockets/simplechat/chatserver.h
+++ b/examples/websockets/simplechat/chatserver.h
@@ -52,21 +52,21 @@
#include <QtCore/QObject>
#include <QtCore/QList>
-#include <QtCore/QByteArray>
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket)
+QT_FORWARD_DECLARE_CLASS(QString)
class ChatServer : public QObject
{
Q_OBJECT
public:
- explicit ChatServer(quint16 port, QObject *parent = Q_NULLPTR);
+ explicit ChatServer(quint16 port, QObject *parent = nullptr);
virtual ~ChatServer();
-private Q_SLOTS:
+private slots:
void onNewConnection();
- void processMessage(QString message);
+ void processMessage(const QString &message);
void socketDisconnected();
private:
diff --git a/examples/websockets/simplechat/main.cpp b/examples/websockets/simplechat/main.cpp
index 0b1c753..a1ffb49 100644
--- a/examples/websockets/simplechat/main.cpp
+++ b/examples/websockets/simplechat/main.cpp
@@ -48,6 +48,7 @@
**
****************************************************************************/
#include <QtCore/QCoreApplication>
+
#include "chatserver.h"
int main(int argc, char *argv[])