-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocketserver.cpp
More file actions
93 lines (76 loc) · 2.05 KB
/
websocketserver.cpp
File metadata and controls
93 lines (76 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <QHash>
#include <iostream>
#include "websocketserver.h"
#include "messagefactory.h"
WebSocketServer::WebSocketServer(QObject* parent)
: QObject(parent)
{
}
WebSocketServer::~WebSocketServer()
{
int x = 0;
}
void WebSocketServer::start(unsigned short port)
{
this->m_server = new QWsServer(this);
if (!this->m_server->listen(QHostAddress::Any, port))
{
std::cout << "An error occurred while starting the server" << std::endl;
}
else
{
connect(this->m_server, SIGNAL(newConnection()), this, SLOT(process_new_connection()));
}
}
void WebSocketServer::stop()
{
this->m_server->close();
}
void WebSocketServer::process_new_connection()
{
QWsSocket* client_socket = m_server->nextPendingConnection();
connect(client_socket, SIGNAL(frameReceived(QString)), this, SLOT(process_message(QString)));
connect(client_socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected()));
connect(client_socket, SIGNAL(pong(quint64)), this, SLOT(process_pong(quint64)));
this->m_clients << client_socket;
}
void WebSocketServer::process_message(QString json_string)
{
QWsSocket* socket = qobject_cast<QWsSocket*>(sender());
if (socket == NULL)
{
return;
}
QHash<QString, QString> json_map = json_to_map(json_string);
MessageHandle message = MessageFactory::CreateMessage(json_map);
QString response = message->HandleMessage();
// Here is where we invoke the mailbox and messaging system
socket->write(response);
}
void WebSocketServer::process_pong(quint64 elapsed_time)
{
}
void WebSocketServer::socket_disconnected()
{
QWsSocket* socket = qobject_cast<QWsSocket*>(sender());
if (socket == NULL)
{
return;
}
this->m_clients.removeOne(socket);
socket->deleteLater();
}
//TODO: complete this function
//This function converts a json string to a QHash
QHash<QString, QString> WebSocketServer::json_to_map(QString json)
{
QHash<QString, QString> map;
return map;
}
//TODO: complete this function
//This function converts a QHash to a json string
QString WebSocketServer::map_to_json(QHash<QString, QString> map)
{
QString json = "";
return json;
}