SignalingServer.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Qt signaling server example for libdatachannel
  3. * Copyright (c) 2022 cheungxiongwei
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #include "SignalingServer.h"
  10. #include <QDebug>
  11. #include <QtWebSockets>
  12. #include <format>
  13. SignalingServer::SignalingServer(QObject *parent) : QObject(parent) {
  14. server = new QWebSocketServer("SignalingServer", QWebSocketServer::NonSecureMode, this);
  15. QObject::connect(server, &QWebSocketServer::newConnection, this,
  16. &SignalingServer::onNewConnection);
  17. }
  18. bool SignalingServer::listen(const QHostAddress &address, quint16 port) {
  19. return server->listen(address, port);
  20. }
  21. void SignalingServer::onNewConnection() {
  22. auto webSocket = server->nextPendingConnection();
  23. auto client_id = webSocket->requestUrl().path().split("/").at(1);
  24. qInfo() << QString::fromStdString(
  25. std::format("Client {} connected", client_id.toUtf8().constData()));
  26. clients[client_id] = webSocket;
  27. webSocket->setObjectName(client_id);
  28. QObject::connect(webSocket, &QWebSocket::disconnected, this, &SignalingServer::onDisconnected);
  29. QObject::connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
  30. this, &SignalingServer::onWebSocketError);
  31. QObject::connect(webSocket, &QWebSocket::binaryMessageReceived, this,
  32. &SignalingServer::onBinaryMessageReceived);
  33. QObject::connect(webSocket, &QWebSocket::textMessageReceived, this,
  34. &SignalingServer::onTextMessageReceived);
  35. }
  36. void SignalingServer::onDisconnected() {
  37. QWebSocket *webSocket = qobject_cast<QWebSocket *>(sender());
  38. clients.remove(webSocket->objectName());
  39. }
  40. void SignalingServer::onWebSocketError(QAbstractSocket::SocketError error) {
  41. qDebug() << QString::fromStdString(std::format("Client {} << {}",
  42. sender()->objectName().toUtf8().constData(),
  43. QString::number(error).toUtf8().constData()));
  44. }
  45. void SignalingServer::onBinaryMessageReceived(const QByteArray &message) {
  46. qInfo() << QString::fromStdString(std::format(
  47. "Client {} << {}", sender()->objectName().toUtf8().constData(), message.constData()));
  48. }
  49. void SignalingServer::onTextMessageReceived(const QString &message) {
  50. QWebSocket *webSocket = qobject_cast<QWebSocket *>(sender());
  51. qInfo() << QString::fromStdString(std::format("Client {} << {}",
  52. webSocket->objectName().toUtf8().constData(),
  53. message.toUtf8().constData()));
  54. auto JsonObject = QJsonDocument::fromJson(message.toUtf8()).object();
  55. auto destination_id = JsonObject["id"].toString();
  56. auto destination_websocket = clients[destination_id];
  57. if (destination_websocket) {
  58. JsonObject["id"] = webSocket->objectName();
  59. auto data = QJsonDocument(JsonObject).toJson(QJsonDocument::Compact);
  60. qInfo() << QString::fromStdString(
  61. std::format("Client {} >> {}", destination_id.toUtf8().constData(), data.constData()));
  62. destination_websocket->sendTextMessage(QString(data));
  63. destination_websocket->flush();
  64. } else {
  65. qInfo() << QString::fromStdString(
  66. std::format("Client {} not found", destination_id.toUtf8().constData()));
  67. }
  68. }