浏览器内使用原生websocket接口的测试如下:
let count = 0; let ws = new WebSocket('ws://localhost:3000/ws/chat'); ws.onopen = function () { console.log(`[CLIENT] open()`); ws.send('Hello!'); }; ws.onmessage = function (message) { console.log(`[CLIENT] Received: ${message}`); count++; if (count > 3) { ws.send('Goodbye!'); ws.close(); } else { setTimeout(() => { ws.send(`Hello, I'm Mr No.${count}!`); }, 1000); } };
Sign in to make a reply
VincentAtlas
浏览器内使用原生websocket接口的测试如下: