-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
37 lines (26 loc) · 811 Bytes
/
client.js
File metadata and controls
37 lines (26 loc) · 811 Bytes
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
var ws = new WebSocket("ws://localhost:8080");
function createScriptTag(data) {
var scriptTag = document.createElement("script");
scriptTag.setAttribute("data-script-url", data.url);
scriptTag.setAttribute("type", data.contentType);
var txt = document.createTextNode(data.data);
scriptTag.appendChild(txt);
return scriptTag;
}
function addDynamicScript(data) {
document.head.appendChild(createScriptTag(data));
}
ws.onopen = function() {
console.log("Connection opened");
// Send that we want to load a script
ws.send("js/jquery-1.11.0.min.js");
ws.send("js/alert.js");
};
ws.onmessage = function(message) {
var data = JSON.parse(message.data);
// Check if javascript
if(data.contentType === "text/javascript") {
addDynamicScript(data);
}
console.log("Received message: ", data);
};