From 33a1e2727cf45cb3e9c865f33b3570b0048af629 Mon Sep 17 00:00:00 2001 From: yangzhao Date: Tue, 25 May 2021 02:53:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E5=AD=98=E5=8D=A0?= =?UTF-8?q?=E7=94=A8=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html.qrc | 2 +- html/xterm.html | 38 ++++++++++++++++++++++++++++++++----- ishell.pro | 2 ++ ishell.pro.user | 2 +- mainwindow.cpp | 4 ++++ mainwindow.h | 2 ++ webconsole.cpp | 20 ++++++++------------ webconsole.h | 1 + websocketserver.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ websocketserver.h | 34 +++++++++++++++++++++++++++++++++ 10 files changed, 132 insertions(+), 19 deletions(-) create mode 100644 websocketserver.cpp create mode 100644 websocketserver.h diff --git a/html.qrc b/html.qrc index 340b468..ed9710f 100644 --- a/html.qrc +++ b/html.qrc @@ -3,12 +3,12 @@ html/qwebchannel.js html/README.html - html/xterm.html html/fit.min.js html/fullscreen.min.css html/fullscreen.min.js html/jquery.min.js html/xterm.min.css html/xterm.min.js + html/xterm.html diff --git a/html/xterm.html b/html/xterm.html index fa4342f..4bb81a6 100644 --- a/html/xterm.html +++ b/html/xterm.html @@ -21,10 +21,12 @@
\ No newline at end of file diff --git a/ishell.pro b/ishell.pro index 24e035f..5ae1a38 100644 --- a/ishell.pro +++ b/ishell.pro @@ -22,6 +22,7 @@ SOURCES += \ mylabel.cpp \ sshclient.cpp \ webconsole.cpp \ + websocketserver.cpp \ welcome.cpp HEADERS += \ @@ -37,6 +38,7 @@ HEADERS += \ mylabel.h \ sshclient.h \ webconsole.h \ + websocketserver.h \ welcome.h FORMS += \ diff --git a/ishell.pro.user b/ishell.pro.user index cb7624a..df5c86e 100644 --- a/ishell.pro.user +++ b/ishell.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/mainwindow.cpp b/mainwindow.cpp index bb1a233..28ee382 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -7,6 +7,7 @@ #include "db/dbutil.h" #include + MainWindow* mainwindow=NULL; MainWindow::MainWindow(QWidget *parent) @@ -22,6 +23,9 @@ MainWindow::MainWindow(QWidget *parent) QDir dir; dir.mkdir(Common::workspacePath); +// WebSocketServer webSocketServer; + webSocketServer=new WebSocketServer(this); + webSocketServer->run(); // int desktop_width = rect.width(); // int desktop_height = rect.height(); diff --git a/mainwindow.h b/mainwindow.h index 09ee523..5473ad6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -6,6 +6,7 @@ #include "connectmanagerui.h" #include "welcome.h" #include +#include "websocketserver.h" #ifdef WIN32 #pragma execution_character_set("utf-8") @@ -38,6 +39,7 @@ private slots: ConnectManagerUI *connectManagerUI; int currentIndex; QWebEngineView* webView; + WebSocketServer* webSocketServer; void initUI(); }; #endif // MAINWINDOW_H diff --git a/webconsole.cpp b/webconsole.cpp index 51ceab7..08807f1 100644 --- a/webconsole.cpp +++ b/webconsole.cpp @@ -5,6 +5,7 @@ #include #include "common.h" #include +#include "websocketserver.h" WebConsole::WebConsole(QWidget *parent,ConnectInfo* connectInfo) : @@ -41,7 +42,8 @@ void WebConsole::resizeEvent(QResizeEvent *) } void WebConsole::pageLoadFinished(bool flag){ - + clientId=QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch()); + webView->page()->runJavaScript("setClientId("+clientId+")"); } void WebConsole::connectSuccess(){ @@ -77,26 +79,18 @@ void WebConsole::ssh2connect(const QString& jsMsg){ connect(sshClient,&SSHClient::readChannelData,this,[&](char data){ - QString script; //处理中文 if(data & 0x80){ ba.append(data); if(ba.length()==3){ QString str(ba); ba.clear(); - script="term.write('"+str+"')"; - webView->page()->runJavaScript(script); + WebSocketServer::instance->sendMsg(clientId,str); } return; } - if(data==13){ - script="lineFeed()"; - }else{ - QString d(data); - script="term.write('"+d+"')"; - } - - webView->page()->runJavaScript(script); + QString d(data); + WebSocketServer::instance->sendMsg(clientId,d); }); } @@ -118,7 +112,9 @@ void WebConsole::setChannelRequestPtySize(const QString& size){ void WebConsole::closeEvent(QCloseEvent *event){ qDebug() << "close window"; sshClient->stop(); + QTimer::singleShot(1000,this,[&](){ + WebSocketServer::deleteClient(clientId); delete this; }); } diff --git a/webconsole.h b/webconsole.h index bd17909..363a2c1 100644 --- a/webconsole.h +++ b/webconsole.h @@ -37,6 +37,7 @@ class WebConsole : public QWidget bool openChannelSeccess=false; int rows; int cols; + QString clientId; public slots: void connectSuccess(); diff --git a/websocketserver.cpp b/websocketserver.cpp new file mode 100644 index 0000000..b33fda1 --- /dev/null +++ b/websocketserver.cpp @@ -0,0 +1,46 @@ +#include "websocketserver.h" +#include +QMap WebSocketServer::clientMap; +WebSocketServer* WebSocketServer::instance=NULL; +WebSocketServer::WebSocketServer(QWidget *parent):QWidget(parent) +{ + instance=this; +} + +void WebSocketServer::run(){ + webSocketServer=new QWebSocketServer("Server",QWebSocketServer::NonSecureMode); + if (!webSocketServer->listen(QHostAddress::LocalHost, 12345)) { + qFatal("Failed to open web socket server."); + QMessageBox::warning(this,"警告","12345端口已被占用"); + return; + } + connect(webSocketServer,&QWebSocketServer::newConnection,this,&WebSocketServer::newConnection); +} + +void WebSocketServer::newConnection(){ + QWebSocket *websocket=webSocketServer->nextPendingConnection(); + if(!websocket){ + return; + } + connect(websocket,&QWebSocket::textMessageReceived,this,[=](const QString& msg){ + clientMap[msg]=websocket; + qDebug() << "新增客户端" << clientMap.count(); + },Qt::QueuedConnection); + + connect(websocket,&QWebSocket::disconnected,this,[=](){ + qDebug() << "客户端退出"; + }); + +} +QWebSocket* WebSocketServer::getClient(QString clientId){ + return clientMap[clientId]; +} + +void WebSocketServer::deleteClient(QString clientId){ + clientMap.remove(clientId); +} + +void WebSocketServer::sendMsg(QString clientId,QString msg){ +// qDebug() << "客户端数量" << clientMap.count(); + clientMap[clientId]->sendTextMessage(msg); +} diff --git a/websocketserver.h b/websocketserver.h new file mode 100644 index 0000000..2a210fa --- /dev/null +++ b/websocketserver.h @@ -0,0 +1,34 @@ +#ifndef WEBSOCKETSERVER_H +#define WEBSOCKETSERVER_H + +#include +#include +#include +class WebSocketServer:public QWidget +{ + Q_OBJECT +public: + WebSocketServer(QWidget *parent); + + static WebSocketServer* instance; + + void run(); + + static QWebSocket* getClient(QString clientId); + + static void deleteClient(QString clientId); + + void sendMsg(QString clientId,QString msg); + +private: + QWebSocketServer* webSocketServer; + static QMap clientMap; + +public Q_SLOTS: + void newConnection(); + +signals: + void sendMessage(const QString& msg); +}; + +#endif // WEBSOCKETSERVER_H