Debian Codegraph @

minimal-examples/ws-server/minimal-ws-server-pmd-corner/mount-origin/example.js

raw
1
2 function get_appropriate_ws_url(extra_url)
3 {
4 var pcol;
5 var u = document.URL;
6
7 /*
8 * We open the websocket encrypted if this page came on an
9 * https:// url itself, otherwise unencrypted
10 */
11
12 if (u.substring(0, 5) === "https") {
13 pcol = "wss://";
14 u = u.substr(8);
15 } else {
16 pcol = "ws://";
17 if (u.substring(0, 4) === "http")
18 u = u.substr(7);
19 }
20
21 u = u.split("/");
22
23 /* + "/xxx" bit is for IE10 workaround */
24
25 return pcol + u[0] + "/" + extra_url;
26 }
27
28 function new_ws(urlpath, protocol)
29 {
30 return new WebSocket(urlpath, protocol);
31 }
32
33 var ws = new Array();
34
35 function conn(n)
36 {
37 ws[n] = new_ws(get_appropriate_ws_url("/" + (n + 1)), "lws-minimal");
38 ws[n].n = n;
39 try {
40 ws[n].onopen = function() {
41 document.getElementById("r").disabled = 0;
42 document.getElementById("status").textContent =
43 document.getElementById("status").textContent + " " +
44 "ws open "+ ws[n].extensions;
45 };
46
47 ws[n].onmessage = function got_packet(msg) {
48 if (typeof msg.data !== "string") {
49 //console.log(msg.data);
50 document.getElementById("r").value =
51 document.getElementById("r").value +
52 ws[n].n + " " + "blob uncompressed length " +
53 msg.data.size + "\n";
54 } else
55 document.getElementById("r").value =
56 document.getElementById("r").value + msg.data + "\n";
57 document.getElementById("r").scrollTop =
58 document.getElementById("r").scrollHeight;
59 };
60
61 ws[n].onclose = function(){
62 document.getElementById("r").disabled = 1;
63 document.getElementById("status").textContent = "ws closed";
64 };
65 } catch(exception) {
66 alert("<p>Error " + exception);
67 }
68 }
69
70 window.addEventListener("load", function() {
71
72 var n;
73
74 /*
75 * we make 5 individual connections. Because if we don't, by default pmd
76 * will reuse its dictionary to make subsequent tests very short.
77 */
78
79 for (n = 0; n < 5; n++)
80 conn(n);
81
82 console.log("load");
83
84 }, false);
85