Skip to content

Commit

Permalink
解决内存占用高
Browse files Browse the repository at this point in the history
  • Loading branch information
yz-java committed May 24, 2021
1 parent 9dacc85 commit 33a1e27
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 19 deletions.
2 changes: 1 addition & 1 deletion html.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<qresource prefix="/">
<file>html/qwebchannel.js</file>
<file>html/README.html</file>
<file>html/xterm.html</file>
<file>html/fit.min.js</file>
<file>html/fullscreen.min.css</file>
<file>html/fullscreen.min.js</file>
<file>html/jquery.min.js</file>
<file>html/xterm.min.css</file>
<file>html/xterm.min.js</file>
<file>html/xterm.html</file>
</qresource>
</RCC>
38 changes: 33 additions & 5 deletions html/xterm.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
<div id="terminal" style="padding: 0;margin: 0;"></div>
</body>
<script type="text/javascript">
new QWebChannel(qt.webChannelTransport, function (channel) {
window.core = channel.objects.core
});

new QWebChannel(qt.webChannelTransport, function(channel) {
window.core = channel.objects.core
})

var clientId
var body_height = $(document).height() - 20;
var body_width = $(document).width();
$("#terminal").height(body_height);
Expand Down Expand Up @@ -99,9 +101,35 @@
core.setChannelRequestPtySize(ss)
}
}
setTimeout(() => {

function setClientId(cId){
clientId=cId
open_websocket()
core.ssh2connect("")
}, 100)
}

function open_websocket() {
if (location.search != "")
var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
else
var baseUrl = "ws://localhost:12345";

var socket = new WebSocket(baseUrl);

socket.onclose = function() {
console.error("web channel closed");
};
socket.onerror = function(error) {
console.error("web channel error: " + error);
};
socket.onopen = function() {
socket.send(clientId);
}
socket.onmessage=function(evt){
var received_msg = evt.data
term.write(received_msg)
}
}
</script>

</html>
2 changes: 2 additions & 0 deletions ishell.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SOURCES += \
mylabel.cpp \
sshclient.cpp \
webconsole.cpp \
websocketserver.cpp \
welcome.cpp

HEADERS += \
Expand All @@ -37,6 +38,7 @@ HEADERS += \
mylabel.h \
sshclient.h \
webconsole.h \
websocketserver.h \
welcome.h

FORMS += \
Expand Down
2 changes: 1 addition & 1 deletion ishell.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.13.1, 2021-05-19T19:14:48. -->
<!-- Written by QtCreator 4.13.1, 2021-05-25T01:43:19. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
4 changes: 4 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "db/dbutil.h"
#include <QDir>


MainWindow* mainwindow=NULL;

MainWindow::MainWindow(QWidget *parent)
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "connectmanagerui.h"
#include "welcome.h"
#include <QWebEngineView>
#include "websocketserver.h"

#ifdef WIN32
#pragma execution_character_set("utf-8")
Expand Down Expand Up @@ -38,6 +39,7 @@ private slots:
ConnectManagerUI *connectManagerUI;
int currentIndex;
QWebEngineView* webView;
WebSocketServer* webSocketServer;
void initUI();
};
#endif // MAINWINDOW_H
Expand Down
20 changes: 8 additions & 12 deletions webconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QDir>
#include "common.h"
#include <QTextCodec>
#include "websocketserver.h"


WebConsole::WebConsole(QWidget *parent,ConnectInfo* connectInfo) :
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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);
});
}

Expand All @@ -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;
});
}
Expand Down
1 change: 1 addition & 0 deletions webconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class WebConsole : public QWidget
bool openChannelSeccess=false;
int rows;
int cols;
QString clientId;

public slots:
void connectSuccess();
Expand Down
46 changes: 46 additions & 0 deletions websocketserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "websocketserver.h"
#include <QMessageBox>
QMap<QString,QWebSocket*> 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);
}
34 changes: 34 additions & 0 deletions websocketserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef WEBSOCKETSERVER_H
#define WEBSOCKETSERVER_H

#include <QWidget>
#include <QWebSocketServer>
#include <QWebSocket>
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<QString,QWebSocket*> clientMap;

public Q_SLOTS:
void newConnection();

signals:
void sendMessage(const QString& msg);
};

#endif // WEBSOCKETSERVER_H

0 comments on commit 33a1e27

Please sign in to comment.