-
Notifications
You must be signed in to change notification settings - Fork 2
/
websocketserver.h
44 lines (36 loc) · 1.07 KB
/
websocketserver.h
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
#ifndef WEBSOCKETSERVER_H
#define WEBSOCKETSERVER_H
#include <QThread>
#include <QtWebSockets/QWebSocket>
#include <QtWebSockets/QWebSocketServer>
#include <mutex>
class WebSocketServer : public QObject {
Q_OBJECT
public:
explicit WebSocketServer(QObject* parent = nullptr);
static WebSocketServer* getInstance() {
std::lock_guard<std::mutex> lock(mutex);
if (instance == nullptr) {
instance = new WebSocketServer();
}
return instance;
}
void start();
quint16 getWebSocketServerPort();
QWebSocket* getWebSocketClient(QString clientId);
signals:
void connectSuccess(QString clientId);
void sendQStringData(QString clientId, QString data);
void sendData(QString clientId, QByteArray data);
private:
QWebSocketServer* server;
static WebSocketServer* instance;
static std::mutex mutex;
int port = 12345;
static QMap<QString, QWebSocket*> clientMap;
private slots:
void onNewConnection();
void writeData(QString clientId, QByteArray data);
void writeQStringData(QString clientId, QString data);
};
#endif // WEBSOCKETSERVER_H