Skip to content

Commit 2dc04ee

Browse files
committed
refactor:重构
1 parent 0f08f3a commit 2dc04ee

File tree

12 files changed

+122
-148
lines changed

12 files changed

+122
-148
lines changed

remote-web/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<dependency>
6666
<groupId>com.jcraft</groupId>
6767
<artifactId>jsch</artifactId>
68-
<version>0.1.54</version>
68+
<version>0.1.55</version>
6969
</dependency>
7070

7171
<dependency>

remote-web/src/main/java/com/jzf/remote/web/config/WebSSHWebSocketConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class WebSSHWebSocketConfig implements WebSocketConfigurer{
2222
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
2323
//socket通道
2424
//指定处理器和路径
25-
webSocketHandlerRegistry.addHandler(webSSHWebSocketHandler, "/webssh")
25+
webSocketHandlerRegistry.addHandler(webSSHWebSocketHandler, "/web/terminal")
2626
.addInterceptors(new WebSocketInterceptor())
2727
.setAllowedOrigins("*");
2828
}

remote-web/src/main/java/com/jzf/remote/web/controller/indexController.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,4 @@ public class indexController {
1212
public String index() {
1313
return "/WebIDE.html";
1414
}
15-
16-
17-
@GetMapping("ssh")
18-
public String webSSH() {
19-
return "/WebSSH.html";
20-
}
21-
2215
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.jzf.remote.web.service.WebSSHService;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
14+
import org.springframework.beans.factory.annotation.Value;
1415
import org.springframework.stereotype.Service;
1516
import org.springframework.web.socket.TextMessage;
1617
import org.springframework.web.socket.WebSocketSession;
@@ -32,13 +33,22 @@
3233
*/
3334
@Service
3435
public class WebSSHServiceImpl implements WebSSHService {
36+
private Logger logger = LoggerFactory.getLogger(WebSSHServiceImpl.class);
37+
3538
//存放ssh连接信息的map
3639
private static Map<String, Object> sshMap = new ConcurrentHashMap<>();
37-
38-
private Logger logger = LoggerFactory.getLogger(WebSSHServiceImpl.class);
3940
//线程池
4041
private ExecutorService executorService = Executors.newCachedThreadPool();
4142

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+
4252
/**
4353
* @Description: 初始化连接
4454
* @Param: [session]
@@ -70,6 +80,10 @@ public void recvHandle(String buffer, WebSocketSession session) {
7080
WebSSHData webSSHData = null;
7181
try {
7282
webSSHData = objectMapper.readValue(buffer, WebSSHData.class);
83+
webSSHData.setHost(host);
84+
webSSHData.setPort(port);
85+
webSSHData.setUsername(username);
86+
webSSHData.setPassword(password);
7387
} catch (IOException e) {
7488
logger.error("Json转换异常");
7589
logger.error("异常信息:{}", e.getMessage());

remote-web/src/main/java/com/jzf/remote/web/websocket/OneWebSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @date 2021/12/15 17:34
2121
*/
2222
@Slf4j
23-
@ServerEndpoint(value = "/test/one")
23+
@ServerEndpoint(value = "/web/console")
2424
@Component
2525
public class OneWebSocket {
2626
/**
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
server:
2-
port: 80
2+
port: 80
3+
4+
terminal:
5+
host: 47.92.137.2
6+
port: 22
7+
username: xxx
8+
password: xxx

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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" />
1920

2021
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
2122
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
@@ -33,6 +34,7 @@
3334
integrity="sha512-GAled7oA9WlRkBaUQlUEgxm37hf43V2KEMaEiWlvBO/ueP2BLvBLKN5tIJu4VZOTwo6Z4XvrojYngoN9dJw2ug=="
3435
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
3536
<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>
3638

3739
<link rel="stylesheet" href="/css/darkly.css" />
3840

@@ -171,7 +173,7 @@
171173
<textarea id="console" class="form-control" placeholder="控制台" readonly></textarea>
172174
</div>
173175
<div class="tab-pane fade" id="nav-terminal" role="tabpanel" aria-labelledby="nav-terminal-tab">
174-
开发中...
176+
<div id="terminal"></div>
175177
</div>
176178
</div>
177179
</div>
@@ -226,7 +228,8 @@ <h5 class="modal-title" id="cloneFromGithubLabel">Clone Project From GitHub</h5>
226228
</div>
227229
</div>
228230
<script src="/js/core.js"></script>
229-
<script src="/js/webSocket.js"></script>
231+
<script src="/js/web-console.js"></script>
232+
<script src="/js/web-terminal.js" charset="utf-8"></script>
230233
</body>
231234

232235
</html>

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function initSize() {
136136

137137
editor.setSize($(".selectorLeft").parent().width() - width, height - 41);
138138

139-
$(".form-control[readonly]").css('height', totalHeight - 40 - height - 40 + 'px');
139+
$("#console").css('height', totalHeight - 40 - height - 40 + 'px');
140140
}
141141

142142

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
1-
var websocket = null;
1+
var webConsole = null;
22
//判断当前浏览器是否支持WebSocket, 主要此处要更换为自己的地址
3-
if ('WebSocket' in window) {
4-
websocket = new WebSocket("ws://localhost/test/one");
3+
if (window.WebSocket) {
4+
if (window.location.protocol == 'https:') {
5+
var protocol = 'wss://';
6+
} else {
7+
var protocol = 'ws://';
8+
}
9+
webConsole = new WebSocket(protocol + "localhost/web/console");
510
} else {
611
alert('Not support websocket');
712
}
813

914
//连接成功建立的回调方法
10-
websocket.onopen = function(event) {
15+
webConsole.onopen = function(event) {
1116
console.log("WebSocket connection successful");
1217
}
1318

1419
//连接关闭的回调方法
15-
websocket.onclose = function() {
20+
webConsole.onclose = function() {
1621
console.log("WebSocket close");
1722
}
1823

1924
//接收到消息的回调方法
20-
websocket.onmessage = function(event) {
25+
webConsole.onmessage = function(event) {
2126
//将消息显示在网页上
2227
printConsole(event.data);
2328
}
2429

2530
//连接发生错误的回调方法
26-
websocket.onerror = function() {
31+
webConsole.onerror = function() {
2732
console.log("WebSocket error");
2833
};
2934

3035
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
3136
window.onbeforeunload = function() {
32-
websocket.close();
37+
webConsole.close();
3338
}
3439

3540
//发送消息
3641
function projectClean() {
37-
websocket.send("{'projectName':'algorithm','goals':['clean']}");
42+
webConsole.send("{'projectName':'algorithm','goals':['clean']}");
3843
}
3944

4045
function projectCompile(){
41-
websocket.send("{'projectName':'algorithm','goals':['compile']}");
46+
webConsole.send("{'projectName':'algorithm','goals':['compile']}");
4247
}
4348

4449
function projectBuild() {
45-
websocket.send("{'projectName':'algorithm','goals':['package']}");
50+
webConsole.send("{'projectName':'algorithm','goals':['package']}");
4651
}
4752

4853
function projectCleanAndBuild() {
49-
websocket.send("{'projectName':'algorithm','goals':['clean','package']}");
54+
webConsole.send("{'projectName':'algorithm','goals':['clean','package']}");
5055
}

0 commit comments

Comments
 (0)