Browse Source

clang-format

cheungxiongwei 3 years ago
parent
commit
b8654a9177

+ 70 - 0
examples/signaling-server-qt/SignalingServer.cpp

@@ -1 +1,71 @@
 #include "SignalingServer.h"
+#include <QDebug>
+#include <QtWebSockets>
+#include <format>
+
+SignalingServer::SignalingServer(QObject *parent) : QObject(parent) {
+	server = new QWebSocketServer("SignalingServer", QWebSocketServer::NonSecureMode, this);
+	QObject::connect(server, &QWebSocketServer::newConnection, this,
+	                 &SignalingServer::onNewConnection);
+}
+
+bool SignalingServer::listen(const QHostAddress &address, quint16 port) {
+	return server->listen(address, port);
+}
+
+void SignalingServer::onNewConnection() {
+	auto webSocket = server->nextPendingConnection();
+	auto client_id = webSocket->requestUrl().path().split("/").at(1);
+	qInfo() << QString::fromStdString(
+	    std::format("Client {} connected", client_id.toUtf8().constData()));
+
+	clients[client_id] = webSocket;
+
+	webSocket->setObjectName(client_id);
+	QObject::connect(webSocket, &QWebSocket::disconnected, this, &SignalingServer::onDisconnected);
+	QObject::connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
+	                 this, &SignalingServer::onWebSocketError);
+	QObject::connect(webSocket, &QWebSocket::binaryMessageReceived, this,
+	                 &SignalingServer::onBinaryMessageReceived);
+	QObject::connect(webSocket, &QWebSocket::textMessageReceived, this,
+	                 &SignalingServer::onTextMessageReceived);
+}
+
+void SignalingServer::onDisconnected() {
+	QWebSocket *webSocket = qobject_cast<QWebSocket *>(sender());
+	clients.remove(webSocket->objectName());
+}
+
+void SignalingServer::onWebSocketError(QAbstractSocket::SocketError error) {
+	qDebug() << QString::fromStdString(std::format("Client {} << {}",
+	                                               sender()->objectName().toUtf8().constData(),
+	                                               QString::number(error).toUtf8().constData()));
+}
+
+void SignalingServer::onBinaryMessageReceived(const QByteArray &message) {
+	qInfo() << QString::fromStdString(std::format(
+	    "Client {} << {}", sender()->objectName().toUtf8().constData(), message.constData()));
+}
+
+void SignalingServer::onTextMessageReceived(const QString &message) {
+	QWebSocket *webSocket = qobject_cast<QWebSocket *>(sender());
+
+	qInfo() << QString::fromStdString(std::format("Client {} << {}",
+	                                              webSocket->objectName().toUtf8().constData(),
+	                                              message.toUtf8().constData()));
+
+	auto JsonObject = QJsonDocument::fromJson(message.toUtf8()).object();
+	auto destination_id = JsonObject["id"].toString();
+	auto destination_websocket = clients[destination_id];
+	if (destination_websocket) {
+		JsonObject["id"] = webSocket->objectName();
+		auto data = QJsonDocument(JsonObject).toJson(QJsonDocument::Compact);
+		qInfo() << QString::fromStdString(
+		    std::format("Client {} >> {}", destination_id.toUtf8().constData(), data.constData()));
+		destination_websocket->sendTextMessage(QString(data));
+		destination_websocket->flush();
+	} else {
+		qInfo() << QString::fromStdString(
+		    std::format("Client {} not found", destination_id.toUtf8().constData()));
+	}
+}

+ 17 - 55
examples/signaling-server-qt/SignalingServer.h

@@ -1,67 +1,29 @@
 #ifndef SIGNALINGSERVER_H
 #define SIGNALINGSERVER_H
 
+#include <QAbstractSocket>
+#include <QByteArray>
+#include <QHostAddress>
 #include <QObject>
-#include <QtWebSockets>
-#include <QDebug>
-#include <format>
+#include <QString>
 
-class SignalingServer : public QObject
-{
-    Q_OBJECT
+class QWebSocketServer;
+class QWebSocket;
+class SignalingServer : public QObject {
+	Q_OBJECT
 public:
-    explicit SignalingServer(QObject *parent = nullptr):QObject(parent) {
-        server = new QWebSocketServer("SignalingServer",QWebSocketServer::NonSecureMode);
-        QObject::connect(server,&QWebSocketServer::newConnection,this,&SignalingServer::onNewConnection);
-    }
-    bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) {
-        return server->listen(address,port);
-    }
+	explicit SignalingServer(QObject *parent = nullptr);
+	bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
 private slots:
-    void onNewConnection() {
-        auto webSocket = server->nextPendingConnection();
-        auto client_id = webSocket->requestUrl().path().split("/").at(1);
-        qInfo() <<  QString::fromStdString(std::format("Client {} connected",client_id.toUtf8().constData()));
+	void onNewConnection();
+	void onDisconnected();
+	void onWebSocketError(QAbstractSocket::SocketError error);
+	void onBinaryMessageReceived(const QByteArray &message);
+	void onTextMessageReceived(const QString &message);
 
-        clients[client_id] = webSocket;
-
-        webSocket->setObjectName(client_id);
-        QObject::connect(webSocket,&QWebSocket::disconnected,this,&SignalingServer::onDisconnected);
-        QObject::connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),this,&SignalingServer::onWebSocketError);
-        QObject::connect(webSocket,&QWebSocket::binaryMessageReceived,this,&SignalingServer::onBinaryMessageReceived);
-        QObject::connect(webSocket,&QWebSocket::textMessageReceived,this,&SignalingServer::onTextMessageReceived);
-    }
-    void onDisconnected(){
-        QWebSocket* webSocket = qobject_cast<QWebSocket*>(sender());
-        clients.remove(webSocket->objectName());
-    }
-    void onWebSocketError(QAbstractSocket::SocketError error){
-        qDebug() << QString::fromStdString(std::format("Client {} << {}",sender()->objectName().toUtf8().constData(),QString::number(error).toUtf8().constData()));
-    }
-    void onBinaryMessageReceived(const QByteArray &message){
-        qInfo() <<  QString::fromStdString(std::format("Client {} << {}",sender()->objectName().toUtf8().constData(),message.constData()));
-    }
-    void onTextMessageReceived(const QString &message){
-        QWebSocket* webSocket = qobject_cast<QWebSocket*>(sender());
-
-        qInfo() <<  QString::fromStdString(std::format("Client {} << {}",webSocket->objectName().toUtf8().constData(),message.toUtf8().constData()));
-
-        auto JsonObject = QJsonDocument::fromJson(message.toUtf8()).object();
-        auto destination_id = JsonObject["id"].toString();
-        auto destination_websocket = clients[destination_id];
-        if(destination_websocket) {
-            JsonObject["id"] = webSocket->objectName();
-            auto data = QJsonDocument(JsonObject).toJson(QJsonDocument::Compact);
-            qInfo() <<  QString::fromStdString(std::format("Client {} >> {}",destination_id.toUtf8().constData(),data.constData()));
-            destination_websocket->sendTextMessage(QString(data));
-            destination_websocket->flush();
-        }else{
-            qInfo() <<  QString::fromStdString(std::format("Client {} not found",destination_id.toUtf8().constData()));
-        }
-    }
 private:
-    QWebSocketServer *server;
-    QMap<QString,QWebSocket*> clients;
+	QWebSocketServer *server;
+	QMap<QString, QWebSocket *> clients;
 };
 
 #endif // SIGNALINGSERVER_H

+ 12 - 13
examples/signaling-server-qt/main.cpp

@@ -1,15 +1,14 @@
-#include <QCoreApplication>
-#include "SignalingServer.h"
+#include "SignalingServer.h"
+#include <QCoreApplication>
 
-int main(int argc, char *argv[])
-{
-    QCoreApplication a(argc, argv);
-    SignalingServer server;
-    if(server.listen(QHostAddress("127.0.0.1"),8000)){
-        qInfo() << "Listening on 127.0.0.1:8000";
-        qInfo() << "server listening on 127.0.0.1:8000";
-    }else{
-        qDebug() << "[Errno 10000] error while attempting to bind on address ('127.0.0.1', 8000)";
-    }
-    return a.exec();
+int main(int argc, char *argv[]) {
+	QCoreApplication a(argc, argv);
+	SignalingServer server;
+	if (server.listen(QHostAddress("127.0.0.1"), 8000)) {
+		qInfo() << "Listening on 127.0.0.1:8000";
+		qInfo() << "server listening on 127.0.0.1:8000";
+	} else {
+		qDebug() << "[Errno 10000] error while attempting to bind on address ('127.0.0.1', 8000)";
+	}
+	return a.exec();
 }