jsp增加websocket
$(function(){registerWebsocket();
});var ws;
function registerWebsocket() {// 注册路径 加上自己的用户名ws = new WebSocket("ws://127.0.0.1:8081/client/notify/test")//连接成功建立的回调方法ws.onopen = function () {ws.send("客户端链接成功");}//接收消息并处理ws.onmessage = function (msg) {$('#receiveData').val(msg.data);}//连接发生错误的回调方法ws.onerror = function () {alert("WebSocket连接发生错误");};//连接关闭的回调方法ws.onclose = function () {alert("WebSocket连接关闭");}//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。window.onbeforeunload = function () {closeWebSocket();}
}function sendWebsocketMsg(str){//发送数据ws.send(str);
}function closeWebSocket() {//关闭WebSocket连接ws.close();
}
后端如何编写看本人文章:https://blog.csdn.net/u010833154/article/details/116712961