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::set_connection_callback(const std::function<void(bool)> &callback)
39 connection_callback = callback;
42 void WSServer::send_command(const string &cmd)
44 for (QWebSocket *sock : clients) {
45 sock->sendTextMessage(QString::fromStdString(cmd));
49 void WSServer::on_new_connection()
51 QWebSocket *sock = ws_server->nextPendingConnection();
52 connect(sock, &QWebSocket::disconnected, this, &WSServer::disconnected);
55 connection_callback(true);
58 void WSServer::disconnected()
60 QWebSocket *sock = qobject_cast<QWebSocket *>(sender());
64 connection_callback(!clients.empty());