-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-websockets.js
47 lines (39 loc) · 1.31 KB
/
client-websockets.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
/// Basic structure from Ian Gilman
/// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
var socketUrl = "ws://< RASPBERRY PI IP ADDRESS >:9999";
window.SocketServer = function(cfg) {
var SupportedSocket = window.WebSocket || window.MozWebSocket,
noOp = function() { return false; },
defaults = {
connectCallback: noOp,
disconnectCallback: noOp,
drawHandler: noOp
};
this.callbacks = (cfg.connectCallback
&& cfg.disconnectCallback
&& cfg.drawHandler)
? cfg
: defaults;
this.connection = (SupportedSocket)
? new SupportedSocket(socketUrl)
: null;
if (socketServer) this.bindMessageHandlers();
};
SocketServer.prototype = {
send: function(method, data) {
if (this.connection && this.connection.readyState != 1) return;
this.connection.send(JSON.stringify({ method: method, data: data }));
},
bindMessageHandlers: function(data) {
this.connection.onopen = function() {
this.callbacks.connectCallback(this);
};
this.connection.onmessage = function(event) {
var envelope = JSON.parse(event.data);
this.callbacks.drawHandler(envelope.method, envelope.data);
};
this.connection.onclose = function(event) {
this.callbacks.disconnectCallback(this);
};
},
};