• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3    <meta charset="utf-8"/>
4    <title>WebSocket Test</title>
5    <script language="javascript" type="text/javascript">
6        var wsUri = "ws://localhost:9090/";
7        var output;
8        function init() {
9            output = document.getElementById("output");
10            testWebSocket();
11        }
12        function testWebSocket() {
13            websocket = new WebSocket(wsUri);
14            websocket.onopen = function (evt) {
15                onOpen(evt)
16            };
17            websocket.onclose = function (evt) {
18                onClose(evt)
19            };
20            websocket.onmessage = function (evt) {
21                onMessage(evt)
22            };
23            websocket.onerror = function (evt) {
24                onError(evt)
25            };
26        }
27        function onOpen(evt) {
28            writeToScreen("CONNECTED");
29            doSend("WebSocket rocks");
30        }
31        function onClose(evt) {
32            writeToScreen("DISCONNECTED");
33        }
34        function onMessage(evt) {
35            writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
36            websocket.close();
37        }
38        function onError(evt) {
39            writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
40        }
41        function doSend(message) {
42            writeToScreen("SENT: " + message);
43            websocket.send(message);
44        }
45        function writeToScreen(message) {
46            var pre = document.createElement("p");
47            pre.style.wordWrap = "break-word";
48            pre.innerHTML = message;
49            output.appendChild(pre);
50        }
51        window.addEventListener("load", init, false);  </script>
52</head>
53<body>
54<h2>WebSocket Test</h2>
55
56<div id="output"></div>
57</body>
58</html>