<!DOCTYPE HTML>
<html> <head> <meta charset="UTF-8"> <title></title> <script> var socket; function init(){ var url="ws://localhost/12345/"; try{ socket=new WebSocket(url); socket.onopen=function(event){ log("连接已建立"+this.readyState); }; socket.onmessage=function(event){ log("<b>接收到的数据:"+event.data+"</b>"); }; socket.οnerrοr=function(event){ log("出错,信息是:"+this.readyState); }; } catch(err){log(err.message);} document.getElementById("log").focus(); } function quit(){ log("已退出"); socket.close; socket=null; } function send(){ var txt=document.getElementById("log"); var msg=txt.value; if(!msg){alert("不能发送空内容");return;} txt.value=""; txt.focus(); try{ socket.send(msg); log('Send:'+msg); } catch(err){load(err.message);} } function log(msg){ document.getElementById("log").innerHTML+="<br/>+msg"; } </script> </head> <body οnlοad="init()"> <div id="log"></div> <input id="msg" type="text"/> <button onClick="send()">发送</button> <button onClick="quit()">退出</button> </body></html>