summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKurt Pattyn <kurt.pattyn@barco.com>2013-08-03 21:30:49 +0200
committerKurt Pattyn <kurt.pattyn@barco.com>2013-08-03 21:30:49 +0200
commit6843fd170453f82ea81e67832df1b67f01e6b10f (patch)
tree2ed25abc172149e3ecb1cb6a4ca60a57ad03504b /examples
parent21eccc325173db1cbfc2fbcca34b4abdeb816254 (diff)
downloadqtwebsockets-6843fd170453f82ea81e67832df1b67f01e6b10f.tar.gz
Added examples for WebSocket usage
Diffstat (limited to 'examples')
-rw-r--r--examples/examples.pro4
-rw-r--r--examples/websocketclient/main.cpp13
-rw-r--r--examples/websocketclient/websocketclient.cpp22
-rw-r--r--examples/websocketclient/websocketclient.h25
-rw-r--r--examples/websocketclient/websocketclient.pro23
-rw-r--r--examples/websocketserver/helloworldserver.cpp56
-rw-r--r--examples/websocketserver/helloworldserver.h28
-rw-r--r--examples/websocketserver/main.cpp12
-rw-r--r--examples/websocketserver/websocketserver.pro23
9 files changed, 206 insertions, 0 deletions
diff --git a/examples/examples.pro b/examples/examples.pro
new file mode 100644
index 0000000..9f9fe6e
--- /dev/null
+++ b/examples/examples.pro
@@ -0,0 +1,4 @@
+TEMPLATE = subdirs
+
+SUBDIRS = websocketclient \
+ websocketserver
diff --git a/examples/websocketclient/main.cpp b/examples/websocketclient/main.cpp
new file mode 100644
index 0000000..9a57b9b
--- /dev/null
+++ b/examples/websocketclient/main.cpp
@@ -0,0 +1,13 @@
+#include <QCoreApplication>
+#include "websocketclient.h"
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication a(argc, argv);
+
+ WebSocketClient client;
+
+ Q_UNUSED(client);
+
+ return a.exec();
+}
diff --git a/examples/websocketclient/websocketclient.cpp b/examples/websocketclient/websocketclient.cpp
new file mode 100644
index 0000000..0bbfa0f
--- /dev/null
+++ b/examples/websocketclient/websocketclient.cpp
@@ -0,0 +1,22 @@
+#include "websocketclient.h"
+#include <QDebug>
+
+WebSocketClient::WebSocketClient(QObject *parent) :
+ QObject(parent),
+ m_webSocket()
+{
+ connect(&m_webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
+ m_webSocket.open(QUrl("ws://localhost:1234"));
+}
+
+void WebSocketClient::onConnected()
+{
+ qDebug() << "Websocket connected";
+ connect(&m_webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)));
+ m_webSocket.send("Hello, world!");
+}
+
+void WebSocketClient::onTextMessageReceived(QString message)
+{
+ qDebug() << "Message received:" << message;
+}
diff --git a/examples/websocketclient/websocketclient.h b/examples/websocketclient/websocketclient.h
new file mode 100644
index 0000000..33458c8
--- /dev/null
+++ b/examples/websocketclient/websocketclient.h
@@ -0,0 +1,25 @@
+#ifndef WEBSOCKETCLIENT_H
+#define WEBSOCKETCLIENT_H
+
+#include <QObject>
+#include "websocket.h"
+
+class WebSocketClient : public QObject
+{
+ Q_OBJECT
+public:
+ explicit WebSocketClient(QObject *parent = 0);
+
+Q_SIGNALS:
+
+public Q_SLOTS:
+
+private Q_SLOTS:
+ void onConnected();
+ void onTextMessageReceived(QString message);
+
+private:
+ WebSocket m_webSocket;
+};
+
+#endif // WEBSOCKETCLIENT_H
diff --git a/examples/websocketclient/websocketclient.pro b/examples/websocketclient/websocketclient.pro
new file mode 100644
index 0000000..c6c4c25
--- /dev/null
+++ b/examples/websocketclient/websocketclient.pro
@@ -0,0 +1,23 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2013-08-03T19:04:16
+#
+#-------------------------------------------------
+
+QT += core
+
+QT -= gui
+
+TARGET = websocketclient
+CONFIG += console
+CONFIG -= app_bundle
+
+TEMPLATE = app
+
+include(../../source/websocket.pri)
+
+SOURCES += main.cpp \
+ websocketclient.cpp
+
+HEADERS += \
+ websocketclient.h
diff --git a/examples/websocketserver/helloworldserver.cpp b/examples/websocketserver/helloworldserver.cpp
new file mode 100644
index 0000000..fa833c0
--- /dev/null
+++ b/examples/websocketserver/helloworldserver.cpp
@@ -0,0 +1,56 @@
+#include "helloworldserver.h"
+#include "websocketserver.h"
+#include "websocket.h"
+#include <QDebug>
+
+HelloWorldServer::HelloWorldServer(quint16 port, QObject *parent) :
+ QObject(parent),
+ m_pWebSocketServer(0),
+ m_clients()
+{
+ m_pWebSocketServer = new WebSocketServer(this);
+ if (m_pWebSocketServer->listen(QHostAddress::Any, port))
+ {
+ qDebug() << "HelloWorld Server listening on port" << port;
+ connect(m_pWebSocketServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
+ }
+}
+
+void HelloWorldServer::onNewConnection()
+{
+ qDebug() << "Client connected.";
+ WebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
+
+ connect(pSocket, SIGNAL(textFrameReceived(QString,bool)), this, SLOT(processMessage(QString, bool)));
+ //connect(pSocket, SIGNAL(binaryFrameReceived(QByteArray,bool)), this, SLOT(processBinaryMessage(QByteArray)));
+ connect(pSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
+ //connect(pSocket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64)));
+
+ m_clients << pSocket;
+}
+
+void HelloWorldServer::processMessage(QString message, bool isLastFrame)
+{
+ Q_UNUSED(isLastFrame);
+ WebSocket *pClient = qobject_cast<WebSocket *>(sender());
+ if (pClient != 0)
+ {
+ QString answer;
+ for (int i = 0; i < message.length(); ++i)
+ {
+ answer.push_front(message[i]);
+ }
+ pClient->send(answer);
+ }
+}
+
+void HelloWorldServer::socketDisconnected()
+{
+ WebSocket *pClient = qobject_cast<WebSocket *>(sender());
+ if (pClient != 0)
+ {
+ qDebug() << "Client disconnected";
+ m_clients.removeAll(pClient);
+ pClient->deleteLater();
+ }
+}
diff --git a/examples/websocketserver/helloworldserver.h b/examples/websocketserver/helloworldserver.h
new file mode 100644
index 0000000..51ce694
--- /dev/null
+++ b/examples/websocketserver/helloworldserver.h
@@ -0,0 +1,28 @@
+#ifndef HELLOWORLDSERVER_H
+#define HELLOWORLDSERVER_H
+
+#include <QObject>
+#include <QList>
+
+class WebSocketServer;
+class WebSocket;
+
+class HelloWorldServer : public QObject
+{
+ Q_OBJECT
+public:
+ explicit HelloWorldServer(quint16 port, QObject *parent = 0);
+
+Q_SIGNALS:
+
+private Q_SLOTS:
+ void onNewConnection();
+ void processMessage(QString message, bool isLastFrame);
+ void socketDisconnected();
+
+private:
+ WebSocketServer *m_pWebSocketServer;
+ QList<WebSocket *> m_clients;
+};
+
+#endif // HELLOWORLDSERVER_H
diff --git a/examples/websocketserver/main.cpp b/examples/websocketserver/main.cpp
new file mode 100644
index 0000000..d5060ae
--- /dev/null
+++ b/examples/websocketserver/main.cpp
@@ -0,0 +1,12 @@
+#include <QCoreApplication>
+#include "helloworldserver.h"
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication a(argc, argv);
+ HelloWorldServer server(1234);
+
+ Q_UNUSED(server);
+
+ return a.exec();
+}
diff --git a/examples/websocketserver/websocketserver.pro b/examples/websocketserver/websocketserver.pro
new file mode 100644
index 0000000..599945c
--- /dev/null
+++ b/examples/websocketserver/websocketserver.pro
@@ -0,0 +1,23 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2013-08-03T19:54:35
+#
+#-------------------------------------------------
+
+QT += core
+
+QT -= gui
+
+TARGET = websocketserver
+CONFIG += console
+CONFIG -= app_bundle
+
+TEMPLATE = app
+
+include(../../source/websocket.pri)
+
+SOURCES += main.cpp \
+ helloworldserver.cpp
+
+HEADERS += \
+ helloworldserver.h