-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameSparksAdminSocket.js
221 lines (185 loc) · 4.76 KB
/
GameSparksAdminSocket.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var crypto = require('crypto');
var WebSocketClient = require('./GameSparksWebSocketClient').WebSocket;
exports.socket = function() {
var appSecret;
var socketUrl;
var locatorUrl;
var initialised;
var connected;
var error;
var closing;
var initCallback;
var messageCallback;
var errorCallback;
var webSocket;
var debug;
var socketNumber;
var pendingRequests = {};
var requestCount = 0;
this.init = function(options) {
socketUrl = options.url;
locatorUrl = options.url;
appSecret = options.secret;
initCallback = options.onInit;
messageCallback = options.onMessage;
errorCallback = options.onError;
debug = options.debug;
socketNumber = options.socketNumber;
initialised = false;
connected = false;
error = false;
closing = false;
cleanup();
connect();
};
this.send = function(requestType, onResponse) {
this.sendWithData(requestType, {}, onResponse);
};
this.ready = function() {
if (debug) {
console.log("I am ready");
}
return connected;
};
this.sendWithData = function(requestType, json, onResponse) {
if (!initialised) {
onResponse({
error : "NOT_INITIALISED"
});
return;
}
if (requestType.indexOf('.') !== 0) {
requestType = "." + requestType;
}
json["@class"] = requestType;
json.requestId = (new Date()).getTime() + "_" + (++requestCount);
//Reset the counter when it gets to 10000
//Assumption here is we won't send this many in a millisecond :)
if(requestCount > 10000){
requestCount = 0;
}
if (onResponse) {
pendingRequests[json.requestId] = onResponse;
setTimeout(function() {
if (pendingRequests[json.requestId]) {
pendingRequests[json.requestId]({
error : "NO_RESPONSE"
});
}
}, 10000);
}
var requestString = JSON.stringify(json);
if (debug) {
console.log("WebSocket send: " + requestString);
}
webSocket.send(requestString);
};
this.close = function() {
cleanup();
};
function cleanup() {
connected = false;
if (webSocket) {
webSocket.onClose = null;
webSocket.close();
}
}
function getWebSocket(location) {
return new WebSocketClient(location);
}
function connect() {
webSocket = getWebSocket(socketUrl);
webSocket.onopen = onWebSocketOpen;
webSocket.onclose = onWebSocketClose;
webSocket.onerror = onWebSocketError;
webSocket.onmessage = onWebSocketMessage;
}
function onWebSocketError(error) {
//On Windows / Azure there is a chance we get an ECONNRESET as well as a close.
if(error && error.code === "ECONNRESET"){
return;
}
socketUrl = locatorUrl;
connected = false;
if (errorCallback && !initialised) {
if (debug) {
console.log('WebSocket onError:' + JSON.stringify(error));
}
errorCallback(error);
}
error = true;
}
function onWebSocketClose(closeEvent) {
connected = false;
if (!error && !initialised) {
connect();
} else {
if (debug) {
console.log('WebSocket onClose executed ' + closeEvent);
}
}
}
function onWebSocketOpen() {
connected = true;
if (debug) {
console.log('WebSocket onOpen: Connected ');
}
}
function onWebSocketMessage(message) {
var messageData = message.data;
if (debug) {
console.log('WebSocket onMessage: ' + messageData);
}
var result;
try {
result = JSON.parse(messageData);
} catch (e) {
console.log("An error ocurred while parsing the JSON Data: "
+ message + "; Error: " + e);
return;
}
var resultType = result['@class'];
if (resultType === ".AuthenticatedConnectResponse") {
handshake(result);
} else if (resultType.match(/Response$/)) {
if (result.requestId) {
var requestId = result.requestId;
if (pendingRequests[requestId]) {
pendingRequests[requestId](result);
pendingRequests[requestId] = null;
}
}
} else {
messageCallback(result);
}
}
function handshake(result) {
if (result.connectUrl) {
socketUrl = result.connectUrl;
return;
} else if (result.nonce) {
var toSend = {
"@class" : ".AuthenticatedConnectRequest",
hmac : crypto.createHmac('SHA256', appSecret).update(
result.nonce).digest('base64')
};
toSend.platform = "node-js";
toSend.os = "node-js";
webSocket.send(JSON.stringify(toSend));
return;
} else if (result.sessionId) {
initialised = true;
if (initCallback) {
initCallback();
}
} else if (result.error) {
var report_error = new Error(JSON.stringify(result.error) + " during handshake, check secret ");
if (errorCallback) {
errorCallback(report_error);
}
else {
throw report_error;
}
}
}
};