Skip to content

Commit 66c4d7a

Browse files
author
Xiong Neng
committed
更新websocket,非Stomp协议的原生WebSocket协议。
1 parent 4b56e79 commit 66c4d7a

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

springboot-websocket/src/main/java/com/xncoding/jwt/handler/SocketHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public void handleTextMessage(WebSocketSession session, TextMessage message) thr
3838
// 我这儿并没有做处理,消息的封装格式一般有{from:xxxx,to:xxxxx,msg:xxxxx},来自哪里,发送给谁,什么消息等等
3939
String msg = message.getPayload();
4040
logger.info("msg = " + msg);
41-
WsParam wsParam = JacksonUtil.json2Bean(msg, new TypeReference<WsParam>(){});
41+
WsParam<String> wsParam = JacksonUtil.json2Bean(msg, new TypeReference<WsParam<String>>(){});
4242
if ("list".equals(wsParam.getMethod())) {
4343
logger.info("call list method...");
44-
WsResponse response = new WsResponse();
44+
WsResponse<String> response = new WsResponse<>();
4545
response.setResult("hello list");
4646
sendMessageToUser(session, new TextMessage(JacksonUtil.bean2Json(response)));
4747
}

springboot-websocket/src/main/java/com/xncoding/jwt/model/WsParam.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* @version 1.0
88
* @since 2018/3/22
99
*/
10-
public class WsParam {
10+
public class WsParam<T> {
1111
private String method;
12-
private Object param;
12+
private T param;
1313

1414
public String getMethod() {
1515
return method;
@@ -19,11 +19,11 @@ public void setMethod(String method) {
1919
this.method = method;
2020
}
2121

22-
public Object getParam() {
22+
public T getParam() {
2323
return param;
2424
}
2525

26-
public void setParam(Object param) {
26+
public void setParam(T param) {
2727
this.param = param;
2828
}
2929
}

springboot-websocket/src/main/java/com/xncoding/jwt/model/WsResponse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* @version 1.0
88
* @since 2018/3/22
99
*/
10-
public class WsResponse {
11-
private Object result;
10+
public class WsResponse<T> {
11+
private T result;
1212

13-
public Object getResult() {
13+
public T getResult() {
1414
return result;
1515
}
1616

17-
public void setResult(Object result) {
17+
public void setResult(T result) {
1818
this.result = result;
1919
}
2020
}

springboot-websocket/src/test/java/com/xncoding/jwt/socket/client/html/index1.html

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
<meta charset="UTF-8"/>
44
<title>广播式WebSocket</title>
55
<script src="js/sockjs.min.js"></script>
6-
<script src="js/stomp.js"></script>
76
<script src="js/jquery-3.1.1.js"></script>
87
</head>
9-
<body onload="disconnect()">
8+
<body>
109
<noscript><h2 style="color: #e80b0a;">Sorry,浏览器不支持WebSocket</h2></noscript>
1110
<div>
1211
<div>
@@ -22,12 +21,50 @@
2221
</div>
2322
</div>
2423
<script type="text/javascript">
24+
var curTryNum = 0;
25+
var url = 'ws://localhost:8282/app';
2526
var ws;
2627
function connect() {
27-
ws = new WebSocket('ws://localhost:8092/app');
28-
ws.onmessage = function(data){
29-
console.log("msg = " + JSON.stringify(data.data));
30-
showResponse(data.data);
28+
ws = new WebSocket(url);
29+
/**
30+
* 因为服务端在 300s 未传输数据时会主动关闭连接,所以,客户端需要定时发送数据保持连接。
31+
*/
32+
var heartCheck = {
33+
timeout: 50000, //50s
34+
timeoutObj: null,
35+
reset: function(){
36+
clearInterval(this.timeoutObj);
37+
this.start();
38+
},
39+
start: function(){
40+
this.timeoutObj = setInterval(function(){
41+
if(ws.readyState === 1){
42+
ws.send("hb");
43+
}
44+
}, this.timeout)
45+
}
46+
};
47+
ws.onopen = function (evnt) {
48+
console.log("onopen: ", evnt);
49+
heartCheck.start();
50+
};
51+
ws.onmessage = function(message){
52+
// 无论收到什么信息,说明当前连接正常,将心跳检测的计时器重置
53+
heartCheck.reset();
54+
console.log("client received a message.data: " + message.data);
55+
if (message.data !== "hb_ok") {
56+
// 不要将ping的答复信息输出
57+
console.log("msg = " + JSON.stringify(message.data));
58+
var response = JSON.parse(message.data);
59+
console.log("message.data.method = " + response.method);
60+
showResponse(JSON.stringify(response.result));
61+
}
62+
};
63+
ws.onclose = function (event) {
64+
if (event.code !== 4500) {
65+
//4500为服务端在打开多tab时主动关闭返回的编码
66+
connect();
67+
}
3168
};
3269
setConnected(true);
3370
}

0 commit comments

Comments
 (0)