1 <!-- |
|
2 Lincense: Public Domain |
|
3 --> |
|
4 |
|
5 <html><head> |
|
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
|
7 <title>Sample of web_socket.js</title> |
|
8 |
|
9 <!-- Include these three JS files: --> |
|
10 <script type="text/javascript" src="swfobject.js"></script> |
|
11 <script type="text/javascript" src="web_socket.js"></script> |
|
12 |
|
13 <script type="text/javascript"> |
|
14 |
|
15 // Set URL of your WebSocketMain.swf here: |
|
16 WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf"; |
|
17 // Set this to dump debug message from Flash to console.log: |
|
18 WEB_SOCKET_DEBUG = true; |
|
19 |
|
20 // Everything below is the same as using standard WebSocket. |
|
21 |
|
22 var ws; |
|
23 |
|
24 function init() { |
|
25 |
|
26 // Connect to Web Socket. |
|
27 // Change host/port here to your own Web Socket server. |
|
28 ws = new WebSocket("ws://localhost:9000/"); |
|
29 |
|
30 // Set event handlers. |
|
31 ws.onopen = function() { |
|
32 output("onopen"); |
|
33 }; |
|
34 ws.onmessage = function(e) { |
|
35 // e.data contains received string. |
|
36 output("onmessage: " + e.data); |
|
37 }; |
|
38 ws.onclose = function() { |
|
39 output("onclose"); |
|
40 }; |
|
41 ws.onerror = function() { |
|
42 output("onerror"); |
|
43 }; |
|
44 |
|
45 } |
|
46 |
|
47 function onSubmit() { |
|
48 var input = document.getElementById("input"); |
|
49 // You can send message to the Web Socket using ws.send. |
|
50 ws.send(input.value); |
|
51 output("send: " + input.value); |
|
52 input.value = ""; |
|
53 input.focus(); |
|
54 } |
|
55 |
|
56 function onCloseClick() { |
|
57 ws.close(); |
|
58 } |
|
59 |
|
60 function output(str) { |
|
61 var log = document.getElementById("log"); |
|
62 var escaped = str.replace(/&/, "&").replace(/</, "<"). |
|
63 replace(/>/, ">").replace(/"/, """); // " |
|
64 log.innerHTML = escaped + "<br>" + log.innerHTML; |
|
65 } |
|
66 |
|
67 </script> |
|
68 </head><body onload="init();"> |
|
69 <form onsubmit="onSubmit(); return false;"> |
|
70 <input type="text" id="input"> |
|
71 <input type="submit" value="Send"> |
|
72 <button onclick="onCloseClick(); return false;">close</button> |
|
73 </form> |
|
74 <div id="log"></div> |
|
75 </body></html> |
|