浏览器控制台测试WebSocket脚本

let ws = new WebSocket("ws://localhost:8080/api/xxx?param1=xxx&param2=xxx");

 

ws.onerror = function () {
console.log("send error!");
}
ws.onopen = function () {
console.log("connection success!<br />")
}
ws.onmessage = function (event) {
console.log(event.data);
}
ws.onclose = function () {
console.log("closed ws!")
}
window.onbeforeunload = function () {
clos();
}
function clos() {
ws.close(3000, "强制关闭");
}

 

ws.send('Hello,WebSocket');

posted @ 2023-10-24 13:39  不懂01的ITer  阅读(265)  评论(0)    收藏  举报