-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.cpp
74 lines (60 loc) · 1.68 KB
/
Server.cpp
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
#include "Server.h"
#include <iostream>
Server::Server(int port, QtWebsocket::Protocol protocol)
{
server = new QtWebsocket::QWsServer(this, protocol);
active = false;
if (! server->listen(QHostAddress::Any, port))
{
std::cout << tr("Error: Can't launch server").toStdString() << std::endl;
std::cout << tr("QWsServer error : %1").arg(server->errorString()).toStdString() << std::endl;
}
else
{
std::cout << tr("Server is listening on port %1").arg(port).toStdString() << std::endl;
}
QObject::connect(server, SIGNAL(newConnection()), this, SLOT(processNewConnection()));
}
Server::~Server()
{
}
void Server::processNewConnection()
{
client = server->nextPendingConnection();
QObject::connect(client, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString)));
QObject::connect(client, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
std::cout << tr("Client connected").toStdString() << std::endl;
connected = true;
emit state(true);
}
void Server::socketDisconnected()
{
QtWebsocket::QWsSocket* socket = qobject_cast<QtWebsocket::QWsSocket*>(sender());
if (socket == 0)
{
return;
}
socket->deleteLater();
std::cout << tr("Client disconnected").toStdString() << std::endl;
connected = false;
emit state(false);
}
void Server::processPressed()
{
QString frame;
frame = "pressed";
if(connected){
if (active == false){
active = true;
client->write(frame);
}
}
}
void Server::processReleased()
{
QString frame;
frame = "released";
if(connected)
client->write(frame);
active = false;
}