Debian Codegraph @

minimal-examples/ws-server/minimal-ws-server-threads-smp/mount-origin/example.js

raw
1 var head = 0, tail = 0, ring = new Array();
2
3 function get_appropriate_ws_url(extra_url)
4 {
5 var pcol;
6 var u = document.URL;
7
8 /*
9 * We open the websocket encrypted if this page came on an
10 * https:// url itself, otherwise unencrypted
11 */
12
13 if (u.substring(0, 5) === "https") {
14 pcol = "wss://";
15 u = u.substr(8);
16 } else {
17 pcol = "ws://";
18 if (u.substring(0, 4) === "http")
19 u = u.substr(7);
20 }
21
22 u = u.split("/");
23
24 /* + "/xxx" bit is for IE10 workaround */
25
26 return pcol + u[0] + "/" + extra_url;
27 }
28
29 function new_ws(urlpath, protocol)
30 {
31 return new WebSocket(urlpath, protocol);
32 }
33
34 document.addEventListener("DOMContentLoaded", function() {
35
36 var ws = new_ws(get_appropriate_ws_url(""), "lws-minimal");
37 try {
38 ws.onopen = function() {
39 document.getElementById("r").disabled = 0;
40 };
41
42 ws.onmessage =function got_packet(msg) {
43 var n, s = "";
44
45 ring[head] = msg.data + "\n";
46 head = (head + 1) % 50;
47 if (tail === head)
48 tail = (tail + 1) % 50;
49
50 n = tail;
51 do {
52 s = s + ring[n];
53 n = (n + 1) % 50;
54 } while (n !== head);
55
56 document.getElementById("r").value = s;
57 document.getElementById("r").scrollTop =
58 document.getElementById("r").scrollHeight;
59 };
60
61 ws.onclose = function(){
62 document.getElementById("r").disabled = 1;
63 };
64 } catch(exception) {
65 alert("<p>Error " + exception);
66 }
67
68 }, false);