连接socket服务器 ~~~ var ws = new WebSocket("ws://localhost:8080/echo"); ~~~ 发送数据到服务器端 ~~~ ws.onopen = function() { ws.send("node.js,您好呀"); alert("数据发送中..."); }; ~~~ 从服务器端接收数据 ~~~ ws.onmessage = function(evt) { var received_msg = evt.data; alert("数据已接收..." + received_msg); console.log(received_msg, JSON.parse(received_msg)); }; ~~~ 关闭websocket ~~~ ws.onclose = function() { // 关闭 websocket alert("连接已关闭..."); }; ~~~