6 #include <sys/socket.h>
12 #include <sys/ioctl.h>
14 #include "QtWebSockets/qwebsocketserver.h"
15 #include "QtWebSockets/qwebsocket.h"
19 WSServer::WSServer(const string &host, int port)
20 : ws_server(new QWebSocketServer("ACMP client", QWebSocketServer::NonSecureMode, this))
22 if (ws_server->listen(QHostAddress::Any, port)) {
23 connect(ws_server, &QWebSocketServer::newConnection, this, &WSServer::on_new_connection);
27 void WSServer::change_port(int port)
29 unordered_set<QWebSocket *> old_clients = move(clients);
30 for (QWebSocket *sock : old_clients) {
34 ws_server->listen(QHostAddress::Any, port);
37 void WSServer::add_init_command(const string &cmd)
39 init_commands.push_back(cmd + "\r\n");
42 void WSServer::set_connection_callback(const std::function<void(bool)> &callback)
44 connection_callback = callback;
47 void WSServer::send_command(const string &cmd)
49 for (QWebSocket *sock : clients) {
50 sock->sendTextMessage(QString::fromStdString(cmd));
54 void WSServer::on_new_connection()
56 QWebSocket *sock = ws_server->nextPendingConnection();
57 connect(sock, &QWebSocket::disconnected, this, &WSServer::disconnected);
59 for (const string &cmd : init_commands) {
60 sock->sendTextMessage(QString::fromStdString(cmd));
63 connection_callback(true);
66 void WSServer::disconnected()
68 QWebSocket *sock = qobject_cast<QWebSocket *>(sender());
72 connection_callback(!clients.empty());