更新websocket,非Stomp协议的原生WebSocket协议。

This commit is contained in:
Xiong Neng 2018-03-22 11:09:56 +08:00
parent 4b56e79eac
commit 66c4d7ae39
4 changed files with 53 additions and 16 deletions

View File

@ -38,10 +38,10 @@ public class SocketHandler extends TextWebSocketHandler {
// 我这儿并没有做处理消息的封装格式一般有{from:xxxx,to:xxxxx,msg:xxxxx}来自哪里发送给谁什么消息等等 // 我这儿并没有做处理消息的封装格式一般有{from:xxxx,to:xxxxx,msg:xxxxx}来自哪里发送给谁什么消息等等
String msg = message.getPayload(); String msg = message.getPayload();
logger.info("msg = " + msg); logger.info("msg = " + msg);
WsParam wsParam = JacksonUtil.json2Bean(msg, new TypeReference<WsParam>(){}); WsParam<String> wsParam = JacksonUtil.json2Bean(msg, new TypeReference<WsParam<String>>(){});
if ("list".equals(wsParam.getMethod())) { if ("list".equals(wsParam.getMethod())) {
logger.info("call list method..."); logger.info("call list method...");
WsResponse response = new WsResponse(); WsResponse<String> response = new WsResponse<>();
response.setResult("hello list"); response.setResult("hello list");
sendMessageToUser(session, new TextMessage(JacksonUtil.bean2Json(response))); sendMessageToUser(session, new TextMessage(JacksonUtil.bean2Json(response)));
} }

View File

@ -7,9 +7,9 @@ package com.xncoding.jwt.model;
* @version 1.0 * @version 1.0
* @since 2018/3/22 * @since 2018/3/22
*/ */
public class WsParam { public class WsParam<T> {
private String method; private String method;
private Object param; private T param;
public String getMethod() { public String getMethod() {
return method; return method;
@ -19,11 +19,11 @@ public class WsParam {
this.method = method; this.method = method;
} }
public Object getParam() { public T getParam() {
return param; return param;
} }
public void setParam(Object param) { public void setParam(T param) {
this.param = param; this.param = param;
} }
} }

View File

@ -7,14 +7,14 @@ package com.xncoding.jwt.model;
* @version 1.0 * @version 1.0
* @since 2018/3/22 * @since 2018/3/22
*/ */
public class WsResponse { public class WsResponse<T> {
private Object result; private T result;
public Object getResult() { public T getResult() {
return result; return result;
} }
public void setResult(Object result) { public void setResult(T result) {
this.result = result; this.result = result;
} }
} }

View File

@ -3,10 +3,9 @@
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
<title>广播式WebSocket</title> <title>广播式WebSocket</title>
<script src="js/sockjs.min.js"></script> <script src="js/sockjs.min.js"></script>
<script src="js/stomp.js"></script>
<script src="js/jquery-3.1.1.js"></script> <script src="js/jquery-3.1.1.js"></script>
</head> </head>
<body onload="disconnect()"> <body>
<noscript><h2 style="color: #e80b0a;">Sorry浏览器不支持WebSocket</h2></noscript> <noscript><h2 style="color: #e80b0a;">Sorry浏览器不支持WebSocket</h2></noscript>
<div> <div>
<div> <div>
@ -22,12 +21,50 @@
</div> </div>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
var curTryNum = 0;
var url = 'ws://localhost:8282/app';
var ws; var ws;
function connect() { function connect() {
ws = new WebSocket('ws://localhost:8092/app'); ws = new WebSocket(url);
ws.onmessage = function(data){ /**
console.log("msg = " + JSON.stringify(data.data)); * 因为服务端在 300s 未传输数据时会主动关闭连接,所以,客户端需要定时发送数据保持连接。
showResponse(data.data); */
var heartCheck = {
timeout: 50000, //50s
timeoutObj: null,
reset: function(){
clearInterval(this.timeoutObj);
this.start();
},
start: function(){
this.timeoutObj = setInterval(function(){
if(ws.readyState === 1){
ws.send("hb");
}
}, this.timeout)
}
};
ws.onopen = function (evnt) {
console.log("onopen: ", evnt);
heartCheck.start();
};
ws.onmessage = function(message){
// 无论收到什么信息,说明当前连接正常,将心跳检测的计时器重置
heartCheck.reset();
console.log("client received a message.data: " + message.data);
if (message.data !== "hb_ok") {
// 不要将ping的答复信息输出
console.log("msg = " + JSON.stringify(message.data));
var response = JSON.parse(message.data);
console.log("message.data.method = " + response.method);
showResponse(JSON.stringify(response.result));
}
};
ws.onclose = function (event) {
if (event.code !== 4500) {
//4500为服务端在打开多tab时主动关闭返回的编码
connect();
}
}; };
setConnected(true); setConnected(true);
} }