纯 Demo 代码,和当年 XMLHttpRequest 似的,裸调 WebSocket:
var socket = new WebSocket('ws://localhost:9501');
socket.onopen = function(event) {
socket.send('HelloWorld!');
socket.onmessage = function(event) {
console.log('Client received a message', event);
};
socket.onclose = function(event) {
console.log('Client notified socket has closed', event);
};
//socket.close();
};