webSocket-服务器可以主动向客户端推送信息

网址:https://blog.csdn.net/wangzhanzheng/article/details/78603532



//消息通知 webcoke(); function webcoke(){ var userId = $(#userId).val(); let host = location.host; if('WebSocket' in window) { console.log(此浏览器支持websocket); this.websocket = new WebSocket(ws+host+ctx+mdrInfoMessageWebSocket+userId); } else if('MozWebSocket' in window) { alert(此浏览器只支持MozWebSocket); } else { alert(此浏览器只支持SockJS); } this.websocket.onopen = (evnt={ console.log(evnt); this.websocket.send(Initial data); }) this.websocket.onmessage = (evnt={ evnt.data返回值 console.log(evnt.data,'链接服务器成功'); $(#message).text(evnt.data); }) this.websocket.onerror = function(evnt) {}; this.websocket.onclose = function(e) { 与服务器断开了链接! console.log('websocket 断开 ' + e.code + ' ' + e.reason + ' ' + e.wasClean) console.log(e) } };

posted @ 2022-08-26 11:06  歇歇吧  阅读(116)  评论(0编辑  收藏  举报