-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameSparksWebSocketClient.js
61 lines (47 loc) · 1.5 KB
/
GameSparksWebSocketClient.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
/**
* New node file
*/
var WebSocketClient = require('./WorlizeWebSocketNode/websocket').client;
exports.WebSocket = function(uri) {
var self = this;
this.connection = null;
this.socket = new WebSocketClient({
keepalive : true,
keepaliveInterval : 20000,
dropConnectionOnKeepaliveTimeout : true,
keepaliveGracePeriod : 5000
});
this.socket.on('connectFailed', function(errorDescription) {
self.onerror("general connection failure\n" + errorDescription + "\n" + (new Error().stack.toString()));
});
this.socket.on('handshakeFailed', function(errorDescription) {
self.onerror("handshake failed\n" + errorDescription + "\n" + (new Error().stack.toString()));
});
this.socket.on('securityFailed', function(errorDescription) {
self.onerror("connect failed, check apiKey\n" + errorDescription + "\n" + (new Error().stack.toString()));
});
this.socket.on('connect', function(connection) {
self.connection = connection;
connection.on('error', function(error) {
self.onerror(error);
});
connection.on('close', function() {
self.onclose();
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
self.onmessage({
data : message.utf8Data
});
}
});
self.onopen();
});
this.socket.connect(uri);
};
exports.WebSocket.prototype.send = function(data) {
this.connection.sendUTF(data);
};
exports.WebSocket.prototype.close = function() {
this.connection.close();
};