123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <html>
- <head>
- <title>Simple client</title>
- <script type="text/javascript">
- var ws;
-
- function init() {
- // Connect to Web Socket
- ws = new WebSocket("ws://localhost:9001/");
- // Set event handlers.
- ws.onopen = function() {
- output("onopen");
- };
-
- ws.onmessage = function(e) {
- // e.data contains received string.
- output("onmessage: " + e.data);
- };
-
- ws.onclose = function() {
- output("onclose");
- };
- ws.onerror = function(e) {
- output("onerror");
- console.log(e)
- };
- }
-
- function onSubmit() {
- var input = document.getElementById("input");
- // You can send message to the Web Socket using ws.send.
- ws.send(input.value);
- output("send: " + input.value);
- input.value = "";
- input.focus();
- }
-
- function onCloseClick() {
- ws.close();
- }
-
- function output(str) {
- var log = document.getElementById("log");
- var escaped = str.replace(/&/, "&").replace(/</, "<").
- replace(/>/, ">").replace(/"/, """); // "
- log.innerHTML = escaped + "<br>" + log.innerHTML;
- }
- </script>
- </head>
- <body onload="init();">
- <form onsubmit="onSubmit(); return false;">
- <input type="text" id="input">
- <input type="submit" value="Send">
- <button onclick="onCloseClick(); return false;">close</button>
- </form>
- <div id="log"></div>
- </body>
- </html>
|