-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.js
41 lines (34 loc) · 875 Bytes
/
ws.js
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
var loc = window.location, new_uri;
if (loc.protocol === "https:") {
new_uri = "wss:";
} else {
new_uri = "ws:";
}
new_uri += "//" + loc.host;
new_uri += "/ws";
window.ws = new WebSocket(new_uri);
window.ws.onopen = function () {
console.log("opened");
}
window.ws.onmessage = function (msg) {
console.log(`received: ${msg.data}`);
document.body.innerHTML = msg.data;
}
const formToJSON = elements => [].reduce.call(elements, (data, element) => {
data[element.name] = element.value;
return data;
}, {});
function sendToProact(name, event) {
event.preventDefault();
let data = {
func: name,
event
};
switch(event.target.tagName) {
case "FORM":
data.fields = formToJSON(event.target.elements);
break;
}
console.log("sending: ", data);
window.ws.send(JSON.stringify(data))
}