diff --git a/client/webrtc_demo/src/mainwindow.cpp b/client/webrtc_demo/src/mainwindow.cpp index 0d4f7d3..0a77343 100644 --- a/client/webrtc_demo/src/mainwindow.cpp +++ b/client/webrtc_demo/src/mainwindow.cpp @@ -3,19 +3,29 @@ #include #include #include "MyCapturer.h" +#include +#include +#include +#include + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) ,mHandler(new rtc::RefCountedObject()) ,mSignalClient(nullptr) + ,mModel(nullptr) { ui->setupUi(this); ui->openGLWidget->SetImgSize(640,480); ui->openGLWidget->show(); mHandler->InitWebrtc(); - mSignalClient = new SignalClient(QUrl("ws://127.0.0.1:9555/ws"),true,this); + TCHAR username[UNLEN + 1]; + DWORD size = UNLEN + 1; + GetUserName((TCHAR*)username, &size); + mModel = new QStandardItemModel(this); + ui->treeView->setModel(mModel); } MainWindow::~MainWindow() @@ -237,3 +247,47 @@ void WebrtcHanlder::OnFailure(webrtc::RTCError error) } + +void MainWindow::on_pushButton_clicked() +{ + mSignalClient = new SignalClient(QUrl("ws://127.0.0.1:9555/ws"),true,this); + connect(this->mSignalClient,SIGNAL(connected()),this,SLOT(signal_conneted())); + connect(this->mSignalClient,SIGNAL(response(int ,QJsonObject)), + this,SLOT( signal_response(int,QJsonObject))); + +} + +void MainWindow::signal_conneted() +{ + ui->label_5->setText("信令服务器已连接"); + this->mSignalClient->SendLogin(); +} + +void MainWindow::on_pushButton_2_clicked() +{ + if(ui->lineEdit->text() != ""){ + if(mSignalClient->Connected()){ + this->mSignalClient->SendInRoom(ui->lineEdit->text()); + this->mSignalClient->SendListRoom(ui->lineEdit->text()); + } + } +} + +void MainWindow::signal_closed() +{ + ui->label_5->setText("信令服务器已断开"); +} + +void MainWindow::signal_response(int type,QJsonObject data) +{ + qDebug()<setEditable(false); + mModel->appendRow(item); + } + break; + } +} diff --git a/client/webrtc_demo/src/mainwindow.h b/client/webrtc_demo/src/mainwindow.h index 6316c07..e3fc966 100644 --- a/client/webrtc_demo/src/mainwindow.h +++ b/client/webrtc_demo/src/mainwindow.h @@ -28,6 +28,7 @@ #include #include "api/video/i420_buffer.h" #include "signal_client.h" +#include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -85,9 +86,16 @@ public slots: void OnUpdateFrame( rtc::scoped_refptr& buffer); void OnUpdateFrame1( uint8_t *); +private slots: + void on_pushButton_clicked(); + void signal_conneted(); + void on_pushButton_2_clicked(); + void signal_closed(); + void signal_response(int,QJsonObject); private: Ui::MainWindow *ui; rtc::scoped_refptr mHandler; SignalClient *mSignalClient; + QStandardItemModel *mModel; }; #endif // MAINWINDOW_H diff --git a/client/webrtc_demo/src/mainwindow.ui b/client/webrtc_demo/src/mainwindow.ui index 4b82978..80f1658 100644 --- a/client/webrtc_demo/src/mainwindow.ui +++ b/client/webrtc_demo/src/mainwindow.ui @@ -14,46 +14,134 @@ MainWindow - + - + + + + 0 + 25 + + 摄像头: - + + + + 0 + 25 + + + + + + 0 + 25 + + 麦克风: - - - - - - Qt::Horizontal - - + + - 40 - 20 + 0 + 25 - + + + + + + 服务器地址: + + + + + + + ws://127.0.0.1:9555/ws + + + + + + + + 0 + 30 + + + + 连接信令服务器 + + + + + + + 房间号: + + + + + + + + + + + 0 + 25 + + + + 加入房间 + + - + + + + + + + + + 0 + 25 + + + + + + + + + + + + 信令服务器未连接 + + + + diff --git a/client/webrtc_demo/src/signal_client.cpp b/client/webrtc_demo/src/signal_client.cpp index 6ebf120..31e336e 100644 --- a/client/webrtc_demo/src/signal_client.cpp +++ b/client/webrtc_demo/src/signal_client.cpp @@ -1,16 +1,79 @@ #include "signal_client.h" - +#include +#include +#include SignalClient::SignalClient(const QUrl &url, bool debug, QObject *parent): QObject(parent), m_url(url), - m_debug(debug) + m_debug(debug), + m_connected(false) { qDebug() << "WebSocket server:" << url; connect(&m_webSocket, &QWebSocket::connected, this, &SignalClient::onConnected); connect(&m_webSocket, &QWebSocket::disconnected, this, &SignalClient::onUnConnected); m_webSocket.open(QUrl(url)); + connect(&m_timer,SIGNAL(timeout()),this,SLOT(onReconenectTimeout())); + m_timer.stop(); + m_timer.setSingleShot(false); +} +SignalClient::~SignalClient() +{ + m_timer.stop(); + m_webSocket.close(); +} + +void SignalClient::SendLogin() +{ + TCHAR username[UNLEN + 1]; + DWORD size = UNLEN + 1; + GetUserName((TCHAR*)username, &size); + + QJsonObject obj; + QJsonObject data; + QJsonDocument doc; + + obj.insert("type",1000); + data.insert("name",QString::fromWCharArray(username)); + obj.insert("data",data); + m_peer_name = QString::fromWCharArray(username); + m_webSocket.sendTextMessage(QJsonDocument(obj).toJson()); +} + +#define REQ_INROOM 1001 +void SignalClient::SendInRoom(QString room) +{ + QJsonObject obj; + QJsonObject data; + data.insert("name",m_peer_name); + data.insert("room_name",room); + + obj.insert("type",REQ_INROOM); + obj.insert("data",data); + m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); +} + +#define REQ_INROOM 1004 +void SignalClient::SendListRoom(QString room) +{ + QJsonObject obj; + QJsonObject data; + data.insert("room_name",room); + + obj.insert("type",REQ_INROOM); + obj.insert("data",data); + m_webSocket.sendTextMessage(QString(QJsonDocument(obj).toJson())); +} + +bool SignalClient::Connected() +{ + return m_connected; +} + +void SignalClient::SetURL(QString url) +{ + m_url = url; } void SignalClient::onConnected() @@ -18,28 +81,34 @@ void SignalClient::onConnected() qDebug() << "WebSocket connected"; connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &SignalClient::onTextMessageReceived); - QJsonObject addr; - QJsonObject dat; - dat.insert("message","hello world"); - addr.insert("type", 1001); - addr.insert("data", dat); - - m_webSocket.sendTextMessage(QJsonDocument(addr).toJson()); + m_connected = true; + m_timer.stop(); + connected(); } void SignalClient::onUnConnected() { qDebug() <<"unconnected"; - + m_timer.start(1000); + m_timer.setSingleShot(false); + m_webSocket.open(QUrl(m_url)); + closed(); } void SignalClient::onTextMessageReceived(QString message) { qDebug() << "Message received:" << message; - m_webSocket.close(); + auto obj = QJsonDocument::fromJson(message.toUtf8()).object(); + qDebug()< #include #include +#include class SignalClient : public QObject { Q_OBJECT public: - explicit SignalClient(const QUrl &url, bool debug = false, QObject *parent = Q_NULLPTR); + explicit SignalClient(const QUrl &url, bool debug = false, + QObject *parent = Q_NULLPTR); + ~SignalClient(); Q_SIGNALS: void closed(); - + void connected(); + void response(int type,QJsonObject); +public: + void SendLogin(); + void SendInRoom(QString ); + void SendListRoom(QString); + bool Connected(); + void SetURL(QString); private Q_SLOTS: void onConnected(); void onUnConnected(); - void onTextMessageReceived(QString message); void onSocketError(QAbstractSocket::SocketError error); + void onReconenectTimeout(); private: + QString m_room_name; // 所属房间名称 + QString m_peer_name; // 节点名称 + QWebSocket m_webSocket; QUrl m_url; bool m_debug; + bool m_connected; + QTimer m_timer; }; + #endif // SIGNALCLIENT_H