-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
62 lines (47 loc) · 1.54 KB
/
client.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(function () {
var make_server_interface = function () {
var web_socket = new WebSocket('ws://' + location.host);
var request_handlers = {};
var handle_request = function (type, handler) {
request_handlers[type] = handler;
};
var send_request = function (request, data) {
var message = JSON.stringify({ request: request, data: data });
web_socket.send(message);
};
web_socket.onmessage = function (event) {
var message = JSON.parse(event.data);
var respond = function (data) {
send_request(message.request, data);
};
request_handlers[message.request](respond, message.data);
};
return {
handle_request: handle_request,
send_request: send_request
};
};
var server_interface = make_server_interface();
var on = server_interface.handle_request;
{ // the 'now check this out' feature
var input = document.getElementById('the-input');
var button = document.getElementById('the-go-button');
on('what is b?', function (respond, data) {
button.innerHTML = data.encouragement;
button.onclick = function () {
respond(parseInt(input.value));
};
});
on('add these please my friend', function (respond, data) {
respond(data.a + data.b);
});
on('now multiply these ones', function (respond, data) {
respond(data.a * data.b);
});
on('proud of yourself?', function (respond, data) {
alert('i said ' + data.you_said + '!');
button.remove();
input.remove();
});
}
})();