2
0

SignalingServer.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef SIGNALINGSERVER_H
  2. #define SIGNALINGSERVER_H
  3. #include <QObject>
  4. #include <QtWebSockets>
  5. #include <QDebug>
  6. #include <format>
  7. class SignalingServer : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit SignalingServer(QObject *parent = nullptr):QObject(parent) {
  12. server = new QWebSocketServer("SignalingServer",QWebSocketServer::NonSecureMode);
  13. QObject::connect(server,&QWebSocketServer::newConnection,this,&SignalingServer::onNewConnection);
  14. }
  15. bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) {
  16. return server->listen(address,port);
  17. }
  18. private slots:
  19. void onNewConnection() {
  20. auto webSocket = server->nextPendingConnection();
  21. auto client_id = webSocket->requestUrl().path().split("/").at(1);
  22. qInfo() << QString::fromStdString(std::format("Client {} connected",client_id.toUtf8().constData()));
  23. clients[client_id] = webSocket;
  24. webSocket->setObjectName(client_id);
  25. QObject::connect(webSocket,&QWebSocket::disconnected,this,&SignalingServer::onDisconnected);
  26. QObject::connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),this,&SignalingServer::onWebSocketError);
  27. QObject::connect(webSocket,&QWebSocket::binaryMessageReceived,this,&SignalingServer::onBinaryMessageReceived);
  28. QObject::connect(webSocket,&QWebSocket::textMessageReceived,this,&SignalingServer::onTextMessageReceived);
  29. }
  30. void onDisconnected(){
  31. QWebSocket* webSocket = qobject_cast<QWebSocket*>(sender());
  32. clients.remove(webSocket->objectName());
  33. }
  34. void onWebSocketError(QAbstractSocket::SocketError error){
  35. qDebug() << QString::fromStdString(std::format("Client {} << {}",sender()->objectName().toUtf8().constData(),QString::number(error).toUtf8().constData()));
  36. }
  37. void onBinaryMessageReceived(const QByteArray &message){
  38. qInfo() << QString::fromStdString(std::format("Client {} << {}",sender()->objectName().toUtf8().constData(),message.constData()));
  39. }
  40. void onTextMessageReceived(const QString &message){
  41. QWebSocket* webSocket = qobject_cast<QWebSocket*>(sender());
  42. qInfo() << QString::fromStdString(std::format("Client {} << {}",webSocket->objectName().toUtf8().constData(),message.toUtf8().constData()));
  43. auto JsonObject = QJsonDocument::fromJson(message.toUtf8()).object();
  44. auto destination_id = JsonObject["id"].toString();
  45. auto destination_websocket = clients[destination_id];
  46. if(destination_websocket) {
  47. JsonObject["id"] = webSocket->objectName();
  48. auto data = QJsonDocument(JsonObject).toJson(QJsonDocument::Compact);
  49. qInfo() << QString::fromStdString(std::format("Client {} >> {}",destination_id.toUtf8().constData(),data.constData()));
  50. destination_websocket->sendTextMessage(QString(data));
  51. destination_websocket->flush();
  52. }else{
  53. qInfo() << QString::fromStdString(std::format("Client {} not found",destination_id.toUtf8().constData()));
  54. }
  55. }
  56. private:
  57. QWebSocketServer *server;
  58. QMap<QString,QWebSocket*> clients;
  59. };
  60. #endif // SIGNALINGSERVER_H