Skip to content

Commit ed1a280

Browse files
ZhiFengJiajiazhifeng
authored andcommitted
fix:修复web终端改变大小时异常
1 parent 2dc04ee commit ed1a280

File tree

24 files changed

+138
-7447
lines changed

24 files changed

+138
-7447
lines changed

remote-web/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
<artifactId>jsch</artifactId>
6868
<version>0.1.55</version>
6969
</dependency>
70+
<!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-core -->
71+
<!-- <dependency>-->
72+
<!-- <groupId>org.apache.sshd</groupId>-->
73+
<!-- <artifactId>sshd-core</artifactId>-->
74+
<!-- <version>2.8.0</version>-->
75+
<!-- </dependency>-->
76+
7077

7178
<dependency>
7279
<groupId>org.springframework.boot</groupId>

remote-web/src/main/java/com/jzf/remote/web/service/impl/WebSSHServiceImpl.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ public class WebSSHServiceImpl implements WebSSHService {
4040
//线程池
4141
private ExecutorService executorService = Executors.newCachedThreadPool();
4242

43-
@Value("${terminal.host}")
44-
private String host;
45-
@Value("${terminal.port}")
46-
private Integer port;
47-
@Value("${terminal.username}")
48-
private String username;
49-
@Value("${terminal.password}")
50-
private String password;
51-
5243
/**
5344
* @Description: 初始化连接
5445
* @Param: [session]
@@ -80,10 +71,6 @@ public void recvHandle(String buffer, WebSocketSession session) {
8071
WebSSHData webSSHData = null;
8172
try {
8273
webSSHData = objectMapper.readValue(buffer, WebSSHData.class);
83-
webSSHData.setHost(host);
84-
webSSHData.setPort(port);
85-
webSSHData.setUsername(username);
86-
webSSHData.setPassword(password);
8774
} catch (IOException e) {
8875
logger.error("Json转换异常");
8976
logger.error("异常信息:{}", e.getMessage());
@@ -103,6 +90,11 @@ public void run() {
10390
} catch (JSchException | IOException e) {
10491
logger.error("webssh连接异常");
10592
logger.error("异常信息:{}", e.getMessage());
93+
try {
94+
session.sendMessage(new TextMessage("连接异常:" + e.getMessage()));
95+
} catch (IOException ex) {
96+
ex.printStackTrace();
97+
}
10698
close(session);
10799
}
108100
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
server:
2-
port: 80
3-
4-
terminal:
5-
host: 47.92.137.2
6-
port: 22
7-
username: xxx
8-
password: xxx
2+
port: 80

remote-web/src/main/resources/static/WebIDE.html

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
integrity="sha512-CCnciBUnVXwa6IQT9q8EmGcarNit9GdKI5nJnj56B1iu0LuD13Qn/GZ+IUkrZROZaBdutN718NK6mIXdUjZGqg=="
1717
crossorigin="anonymous" referrerpolicy="no-referrer" />
1818
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
19-
<link rel="stylesheet" href="/xterm/xterm.css" />
19+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/xterm.min.css"
20+
integrity="sha512-yqkIsVYKEsENpXQX0zLrKj6n50rmX5X1j0cclmCnETFoWG2PKMZjvDEp6do28gGLxIvMnihpERBGRa8Ck0Ls8g=="
21+
crossorigin="anonymous" referrerpolicy="no-referrer" />
2022

2123
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
2224
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
@@ -34,7 +36,12 @@
3436
integrity="sha512-GAled7oA9WlRkBaUQlUEgxm37hf43V2KEMaEiWlvBO/ueP2BLvBLKN5tIJu4VZOTwo6Z4XvrojYngoN9dJw2ug=="
3537
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
3638
<script type='text/javascript' src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
37-
<script src="/xterm/xterm.js" charset="utf-8"></script>
39+
<script src="https://cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/xterm.min.js"
40+
integrity="sha512-S1ITNcRgtLq/tVnMcX7Qcss09kJ5Lu7dZbtXw/dBDjxYJSdyucbYKOYI1nSbb9EtrjEK3h7Kxekkbg4gVLr8gg=="
41+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
42+
<script src="https://cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/addons/fit/fit.min.js"
43+
integrity="sha512-PyTGariw9ceX9zejvpqCikObn0A3cwR3LoDQY1/L+NET1LZBMYi/IPIn6pvfRt02etYhRw4zJ2iU3yGyEH5+8A=="
44+
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
3845

3946
<link rel="stylesheet" href="/css/darkly.css" />
4047

@@ -160,7 +167,7 @@
160167
<div class="row">
161168
<div class="col">
162169
<nav>
163-
<div class="nav nav-tabs" id="nav-tab" role="tablist">
170+
<div class="nav nav-tabs" id="terminal-tab" role="tablist">
164171
<a class="nav-link active" id="nav-console-tab" data-toggle="tab" href="#nav-console" role="tab"
165172
aria-controls="nav-console" aria-selected="true">Console</a>
166173
<a class="nav-link" id="nav-terminal-tab" data-toggle="tab" href="#nav-terminal" role="tab"
@@ -215,9 +222,9 @@ <h5 class="modal-title" id="cloneFromGithubLabel">Clone Project From GitHub</h5>
215222
<div class="modal-body">
216223
<div class="input-group mb-3">
217224
<div class="input-group-prepend">
218-
<span class="input-group-text" id="basic-addon3">URL:</span>
225+
<span class="input-group-text" id="basic-addon">URL:</span>
219226
</div>
220-
<input type="text" class="form-control" aria-describedby="basic-addon3">
227+
<input type="text" class="form-control" aria-describedby="basic-addon">
221228
</div>
222229
</div>
223230
<div class="modal-footer">
@@ -227,9 +234,52 @@ <h5 class="modal-title" id="cloneFromGithubLabel">Clone Project From GitHub</h5>
227234
</div>
228235
</div>
229236
</div>
230-
<script src="/js/core.js"></script>
237+
238+
<!-- Modal -->
239+
<div class="modal fade" id="sshInfo" tabindex="-1" aria-labelledby="sshInfoLabel"
240+
aria-hidden="true">
241+
<div class="modal-dialog modal-dialog-centered">
242+
<div class="modal-content">
243+
<div class="modal-header">
244+
<h5 class="modal-title" id="sshInfoLabel">配置ssh连接信息</h5>
245+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
246+
<span aria-hidden="true">&times;</span>
247+
</button>
248+
</div>
249+
<div class="modal-body">
250+
<div class="input-group mb-3">
251+
<div class="input-group-prepend">
252+
<span class="input-group-text" id="basic-addon1">Host:</span>
253+
</div>
254+
<input type="text" class="form-control" aria-describedby="basic-addon1" placeholder="47.92.137.2" value="47.92.137.2">
255+
<div class="input-group-prepend">
256+
<span class="input-group-text" id="basic-addon2">Port:</span>
257+
</div>
258+
<input type="text" class="form-control" aria-describedby="basic-addon2" placeholder="22" value="22">
259+
</div>
260+
<div class="input-group mb-3">
261+
<div class="input-group-prepend">
262+
<span class="input-group-text" id="basic-addon3">UserName:</span>
263+
</div>
264+
<input type="text" class="form-control" aria-describedby="basic-addon3">
265+
</div>
266+
<div class="input-group mb-3">
267+
<div class="input-group-prepend">
268+
<span class="input-group-text" id="basic-addon4">Password:</span>
269+
</div>
270+
<input type="password" class="form-control" aria-describedby="basic-addon4">
271+
</div>
272+
</div>
273+
<div class="modal-footer">
274+
<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="connectSSH()">Connect</button>
275+
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cannel</button>
276+
</div>
277+
</div>
278+
</div>
279+
</div>
231280
<script src="/js/web-console.js"></script>
232281
<script src="/js/web-terminal.js" charset="utf-8"></script>
282+
<script src="/js/core.js"></script>
233283
</body>
234284

235285
</html>

remote-web/src/main/resources/static/js/core.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ $(function () {
6363
editor.setSize(width, height - 41);
6464

6565
var totalHeight = $(window).height();
66-
$(".form-control[readonly]").css('height', totalHeight - 40 - height - 40 + 'px');
66+
$("#console").css('height', totalHeight - 40 - height - 40 + 'px');
67+
$("#terminal").css('height', totalHeight - 40 - height - 40 + 'px');
68+
69+
terminalResize();
6770
},
6871
stop: function (event, ui) {
6972
var ele = ui.element;
@@ -75,7 +78,10 @@ $(function () {
7578
editor.setSize(width, height - 41);
7679

7780
var totalHeight = $(window).height();
78-
$(".form-control[readonly]").css('height', totalHeight - 40 - height - 40 + 'px');
81+
$("#console").css('height', totalHeight - 40 - height - 40 + 'px');
82+
$("#terminal").css('height', totalHeight - 40 - height - 40 + 'px');
83+
84+
terminalResize();
7985
}
8086
});
8187

@@ -92,7 +98,10 @@ $(function () {
9298
editor.setSize(width, height - 41);
9399

94100
var totalHeight = $(window).height();
95-
$(".form-control[readonly]").css('height', totalHeight - 40 - height - 40 + 'px');
101+
$("#console").css('height', totalHeight - 40 - height - 40 + 'px');
102+
$("#terminal").css('height', totalHeight - 40 - height - 40 + 'px');
103+
104+
terminalResize();
96105
},
97106
stop: function (event, ui) {
98107
var ele = ui.element;
@@ -105,7 +114,10 @@ $(function () {
105114
editor.setSize(width, height - 41);
106115

107116
var totalHeight = $(window).height();
108-
$(".form-control[readonly]").css('height', totalHeight - 40 - height - 40 + 'px');
117+
$("#console").css('height', totalHeight - 40 - height - 40 + 'px');
118+
$("#terminal").css('height', totalHeight - 40 - height - 40 + 'px');
119+
120+
terminalResize();
109121
}
110122
});
111123

@@ -114,7 +126,17 @@ $(function () {
114126
window.addEventListener("resize", () => {
115127
//窗口改变
116128
initSize();
129+
terminalResize();
117130
});
131+
132+
$("#terminal-tab").on('shown.bs.tab', function (event) {
133+
if($(event.target).attr("id") == "nav-terminal-tab"){
134+
if(!isSSHConnected()){
135+
$("#sshInfo").modal('show');
136+
}
137+
terminalResize();
138+
}
139+
})
118140
})
119141

120142
function initSize() {
@@ -137,6 +159,7 @@ function initSize() {
137159
editor.setSize($(".selectorLeft").parent().width() - width, height - 41);
138160

139161
$("#console").css('height', totalHeight - 40 - height - 40 + 'px');
162+
$("#terminal").css('height', totalHeight - 40 - height - 40 + 'px');
140163
}
141164

142165

remote-web/src/main/resources/static/js/web-console.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ if (window.WebSocket) {
1313

1414
//连接成功建立的回调方法
1515
webConsole.onopen = function(event) {
16-
console.log("WebSocket connection successful");
16+
console.log("Web Console connection successful");
1717
}
1818

1919
//连接关闭的回调方法
2020
webConsole.onclose = function() {
21-
console.log("WebSocket close");
21+
console.log("Web Console close");
2222
}
2323

2424
//接收到消息的回调方法
@@ -29,7 +29,7 @@ webConsole.onmessage = function(event) {
2929

3030
//连接发生错误的回调方法
3131
webConsole.onerror = function() {
32-
console.log("WebSocket error");
32+
console.log("Web Console error");
3333
};
3434

3535
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var webSSH = null;
2-
var term = null;
2+
var term = null;
33
//判断当前浏览器是否支持WebSocket, 主要此处要更换为自己的地址
44
if (window.WebSocket) {
55
if (window.location.protocol == 'https:') {
@@ -13,11 +13,11 @@ if (window.WebSocket) {
1313
}
1414

1515
//连接成功建立的回调方法
16-
webSSH.onopen = function(event) {
17-
console.log("WebSocket connection successful");
16+
webSSH.onopen = function (event) {
17+
console.log("Web Terminal connection successful");
1818
term = new Terminal({
1919
cols: 180,
20-
rows: 5,
20+
rows: 1,
2121
cursorBlink: true, // 光标闪烁
2222
cursorStyle: "block", // 光标样式 null | 'block' | 'underline' | 'bar'
2323
scrollback: 800, //回滚
@@ -31,43 +31,57 @@ webSSH.onopen = function(event) {
3131
term.open(document.getElementById('terminal'));
3232
//在页面上显示连接中...
3333
term.write('Connecting...\r\n');
34-
webSSH.send(JSON.stringify({"operate":'connect', "host": '0.0.0.0', "port": '22', "username": 'xxx', "password": 'xxx'}));
3534
}
3635

3736
//连接关闭的回调方法
38-
webSSH.onclose = function() {
39-
console.log("WebSocket close");
37+
webSSH.onclose = function () {
38+
console.log("Web Terminal close");
4039
term.write("\r\nconnection closed");
4140
}
4241

4342
//接收到消息的回调方法
44-
webSSH.onmessage = function(event) {
43+
webSSH.onmessage = function (event) {
4544
//将消息显示在网页上
4645
term.write(event.data);
4746
}
4847

4948
//连接发生错误的回调方法
50-
webSSH.onerror = function(error) {
51-
console.log("WebSocket error");
49+
webSSH.onerror = function (error) {
50+
console.log("Web Terminal error");
5251
term.write('\r\nError: ' + error);
5352
};
5453

5554
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
56-
window.onbeforeunload = function() {
55+
window.onbeforeunload = function () {
5756
webSSH.close();
5857
}
5958

60-
$(".selectorRight").on("resize", function (event, ui) {
61-
var totalWidth = $(window).width();
62-
var totalHeight = $(window).height();
63-
var height = ui.element.height();
59+
function sendClientData(data) {
60+
//发送指令
61+
webSSH.send(JSON.stringify({ "operate": "command", "command": data }));
62+
}
63+
64+
function connectSSH() {
65+
//连接ssh,暂时没用到,目前直接在后端配置文件中配置连接信息.
66+
var host = $("#sshInfo input").get(0).value;
67+
var port = $("#sshInfo input").get(1).value;
68+
var username = $("#sshInfo input").get(2).value;
69+
var password = $("#sshInfo input").get(3).value;
70+
webSSH.send(JSON.stringify({ "operate": 'connect', "host": host, "port": port, "username": username, "password": password }));
71+
isConnected = true;
72+
}
6473

65-
console.log("resize:" + (totalHeight - 40 - height - 40) / 24);
66-
term.resize(180,(totalHeight - 40 - height - 40) / 24);
74+
var isConnected = false;
75+
function isSSHConnected() {
76+
return isConnected;
77+
}
6778

68-
});
79+
function terminalResize(){
80+
var totalWidth = $(window).width();
81+
var totalHeight = $(window).height();
82+
var height = $(".selectorRight").height();
6983

70-
function sendClientData(data) {
71-
//发送指令
72-
webSSH.send(JSON.stringify({"operate": "command", "command": data}));
84+
term.resize(180, (totalHeight - 40 - height - 40) / 24);
85+
term.fit();
86+
console.log("Web Terminal resize:",term.cols, term.rows);
7387
}

0 commit comments

Comments
 (0)