File: example.js

package info (click to toggle)
libwebsockets 4.3.5-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 31,288 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (62 lines) | stat: -rw-r--r-- 1,435 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

function get_appropriate_ws_url(extra_url)
{
	var pcol;
	var u = document.URL;

	/*
	 * We open the websocket encrypted if this page came on an
	 * https:// url itself, otherwise unencrypted
	 */

	if (u.substring(0, 5) === "https") {
		pcol = "wss://";
		u = u.substr(8);
	} else {
		pcol = "ws://";
		if (u.substring(0, 4) === "http")
			u = u.substr(7);
	}

	u = u.split("/");

	/* + "/xxx" bit is for IE10 workaround */

	return pcol + u[0] + "/" + extra_url;
}

function new_ws(urlpath, protocol)
{
	return new WebSocket(urlpath, protocol);
}

document.addEventListener("DOMContentLoaded", function() {

	var ws = new_ws(get_appropriate_ws_url(""), "lws-minimal-pmd-bulk");
	try {
		ws.onopen = function() {
			document.getElementById("r").disabled = 0;
			document.getElementById("status").textContent = "ws open "+ ws.extensions;
		};
	
		ws.onmessage = function got_packet(msg) {
			console.log("Received ws message len " + msg.data.size);
			document.getElementById("r").value =
				document.getElementById("r").value + "\nReceived: " + msg.data.size + " bytes\n";
			document.getElementById("r").scrollTop =
				document.getElementById("r").scrollHeight;
	
			/* echo it back */
			ws.send(msg.data);
		};
	
		ws.onclose = function(){
			document.getElementById("r").disabled = 1;
			document.getElementById("status").textContent = "ws closed";
		};
	} catch(exception) {
		alert("<p>Error " + exception);  
	}

}, false);