|
23 | 23 |
|
24 | 24 | var ws; |
25 | 25 |
|
26 | | - function connect() { |
| 26 | + $("uriForm").observe("submit", function(e) { |
| 27 | + e.stop(); |
27 | 28 | ws = new WebSocket($F("uri")); |
28 | 29 | ws.onopen = function() { |
29 | | - log("[WebSocket#onopen] called.\n"); |
30 | | - $("uriForm").enable(); |
| 30 | + log("[WebSocket#onopen]\n"); |
31 | 31 | } |
32 | 32 | ws.onmessage = function(e) { |
33 | | - log("[WebSocket#onmessage] called.\n\tMessage: " + e.data + "\n"); |
| 33 | + log("[WebSocket#onmessage] Message: '" + e.data + "'\n"); |
34 | 34 | } |
35 | 35 | ws.onclose = function() { |
36 | | - log("[WebSocket#onclose] called.\n"); |
37 | | - $("uriForm").disable(); |
| 36 | + log("[WebSocket#onclose]\n"); |
| 37 | + $("uri", "connect").invoke("enable"); |
| 38 | + $("disconnect").disable(); |
38 | 39 | ws = null; |
39 | 40 | } |
40 | | - } |
| 41 | + $("uri", "connect").invoke("disable"); |
| 42 | + $("disconnect").enable(); |
| 43 | + }); |
41 | 44 |
|
42 | | - function send() { |
| 45 | + $("sendForm").observe("submit", function(e) { |
| 46 | + e.stop(); |
43 | 47 | if (ws) { |
44 | 48 | var textField = $("textField"); |
45 | 49 | ws.send(textField.value); |
46 | | - log("[WebSocket#send] called.\n\tSent string: " + textField.value + "\n"); |
| 50 | + log("[WebSocket#send] Send: '" + textField.value + "'\n"); |
47 | 51 | textField.value = ""; |
48 | 52 | textField.focus(); |
49 | 53 | } |
50 | | - } |
51 | | - |
52 | | - $("uriForm").observe("submit", function(e) { |
53 | | - e.stop(); |
54 | | - connect(); |
55 | 54 | }); |
56 | 55 |
|
57 | | - $("sendForm").observe("submit", function(e) { |
| 56 | + $("disconnect").observe("click", function(e) { |
58 | 57 | e.stop(); |
59 | | - send(); |
60 | | - }); |
61 | | - |
62 | | - $("disconnect").observe("click", function() { |
63 | 58 | if (ws) { |
64 | 59 | ws.close(); |
65 | 60 | } |
|
68 | 63 | </script> |
69 | 64 | </head> |
70 | 65 | <body> |
71 | | - <form id="uriForm"><input type="text" id="uri" value="ws://localhost" style="width:200px;"> <input type="submit" value="Connect"><input type="button" id="disconnect" value="Disconnect"></form><br> |
| 66 | + <form id="uriForm"><input type="text" id="uri" value="ws://localhost" style="width:200px;"> <input type="submit" id="connect" value="Connect"><input type="button" id="disconnect" value="Disconnect" disabled="disabled"></form><br> |
72 | 67 | <form id="sendForm"><input type="text" id="textField" value="" style="width:200px;"> <input type="submit" value="Send"></form><br> |
73 | | - <textarea id="log" rows="50" cols="100"></textarea><br> |
| 68 | + <form><textarea id="log" rows="30" cols="100" style="font-family:monospace; color:red;"></textarea></form><br> |
74 | 69 | </body> |
75 | 70 | </html> |
0 commit comments