1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title></title>
5 </head>
6 <body>
7 <div id="socket">
8 </div>
9 <script type="text/javascript">
10 var o = document.getElementById("socket");
11 // 创建一个Socket实例
12 var socket = new WebSocket('ws://10.10.10.1:8050/');
13
14 // 打开Socket
15 socket.onopen = function (event) {
16
17 // 发送一个初始化消息
18 socket.send('I am the client and I\'m listening!');
19
20 // 监听消息
21 socket.onmessage = function (event) {
22 console.log('Client received a message', event);
23 o.innerHTML += "Client received a message:<br/>" + event;
24 };
25
26 // 监听Socket的关闭
27 socket.onclose = function (event) {
28 console.log('Client notified socket has closed', event);
29 };
30
31 // 关闭Socket....
32 //socket.close()
33 };
34 </script>
35 </body>
36 </html>