-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-client.js
More file actions
117 lines (105 loc) · 2.72 KB
/
web-client.js
File metadata and controls
117 lines (105 loc) · 2.72 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var clientName = 'Alice';
var client;
var deviceId = 'deviceId' + Math.floor(Math.random() * 1000);
function logToPage(message) {
var board = document.getElementById('board');
board.innerHTML += message + '<br/>';
}
function disconnect() {
if (client) {
client.close();
logToPage('Closed connection.');
client = null;
}
}
function createRoom() {
if (client) {
sendCreateMessage();
logToPage('Request create room.');
}
}
function enrollRoom() {
var locator = document.getElementById('locator').value;
if (client) {
sendEnrollMessage(locator);
logToPage('Request enroll to room ' + locator);
}
}
function connect() {
clientName = document.getElementById('name').value;
var server = document.getElementById('server').value;
var protocol = document.getElementById('protocol').value;
// client = new WebSocket('ws://localhost:8080/', 'esgrima');
client = new WebSocket(server, protocol);
client.onerror = function () {
logToPage('Connection Error');
};
client.onopen = function () {
logToPage('WebSocket Client Connected. ID: ' + clientName);
var payload = JSON.stringify({
type: 'HELO',
ts: new Date().toISOString(),
clientId: deviceId,
userId: clientName,
version: '0.0.1'
});
client.send(payload);
};
client.onclose = function () {
let message = 'esgrima-protocol Client Closed. ID: ' + clientName;
console.log(message);
logToPage(message);
};
client.onmessage = function (e) {
if (typeof e.data === 'string') {
console.log("Received: '" + e.data + "'");
const c = JSON.parse(e.data);
const pretty = `${c.ts} ${c.clientId}: from ${c.userId}: ${
c?.locator || ''
} ${c.type || ''} ${c?.payload || ''}`;
logToPage(pretty);
if (c.type == 'CACK') {
document.getElementById('locator').value = c.locator;
}
}
};
}
function sendAddMessage() {
var message = document.getElementById('message').value;
var locator = document.getElementById('locator').value;
logToPage('Local Addition to locator: ' + locator + ' data: ' + message);
var msg = {
type: 'ADD',
ts: new Date().toISOString(),
clientId: deviceId,
userId: clientName,
locator,
payload: message
};
sendMessage(msg);
}
function sendCreateMessage() {
var msg = {
type: 'CREA',
ts: new Date().toISOString(),
clientId: deviceId,
userId: clientName,
initialModel: {}
};
sendMessage(msg);
}
function sendEnrollMessage(locator) {
var msg = {
type: 'ENRO',
ts: new Date().toISOString(),
clientId: deviceId,
userId: clientName,
locator
};
sendMessage(msg);
}
function sendMessage(msg) {
if (client) {
client.send(JSON.stringify(msg));
}
}