window.CHAT = {
socket: null,
init: function() {
if (window.WebSocket) {
CHAT.socket = new WebSocket("websocket地址");
CHAT.socket.onopen = function() {
console.log("连接建立成功...");
},
CHAT.socket.onclose = function() {
console.log("连接关闭...");
},
CHAT.socket.onerror = function() {
console.log("发生错误...");
},
CHAT.socket.onmessage = function(e) {
console.log("接受到消息:" + e.data);
}
} else {
alert("浏览器不支持websocket协议...");
}
},
chat: function() {
CHAT.socket.send('发送的消息');
}
};
前端页面在进行长连接时websocket的写法
最新推荐文章于 2024-07-31 12:06:54 发布