/
MessageSocket.js
242 lines (199 loc) · 8.48 KB
/
MessageSocket.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
*
* WebSocket class use for communication between client and server
*
* @author Jesus R. Martinez (jesus@metacell.us)
*/
define(function (require) {
return function (GEPPETTO) {
var messageHandlers = [];
var clientID = null;
var nextID = 0;
var connectionInterval = 300;
var pako = require("pako");
var FileSaver = require('file-saver');
var callbackHandler = {};
/**
* Web socket creation and communication
*/
GEPPETTO.MessageSocket = {
socket: null,
//sets protocol to use for connection
protocol: GEPPETTO_CONFIGURATION.useSsl ? "wss://" : "ws://",
//flag used to connect using ws protocol if wss failed
failsafe: false,
connect: function (host) {
if ('WebSocket' in window) {
GEPPETTO.MessageSocket.socket = new WebSocket(host);
GEPPETTO.MessageSocket.socket.binaryType = "arraybuffer";
}
else if ('MozWebSocket' in window) {
GEPPETTO.MessageSocket.socket = new MozWebSocket(host);
}
else {
GEPPETTO.CommandController.log(GEPPETTO.Resources.WEBSOCKET_NOT_SUPPORTED, true);
return;
}
GEPPETTO.MessageSocket.socket.onopen = function () {
GEPPETTO.CommandController.log(GEPPETTO.Resources.WEBSOCKET_OPENED, true);
//attach the handlers once socket is opened
messageHandlers.push(GEPPETTO.SimulationHandler);
messageHandlers.push(GEPPETTO.GlobalHandler);
};
GEPPETTO.MessageSocket.socket.onclose = function () {
GEPPETTO.CommandController.log(GEPPETTO.Resources.WEBSOCKET_CLOSED, true);
};
GEPPETTO.MessageSocket.socket.onmessage = function (msg) {
var messageData = msg.data;
if (messageData == "ping") {
return;
}
// if it's a binary (possibly compressed) then determine its type and process it
if (messageData instanceof ArrayBuffer) {
processBinaryMessage(messageData);
// otherwise, for a text message, parse it and notify listeners
} else {
// a non compresed message
parseAndNotify(messageData);
}
};
//Detects problems when connecting to Geppetto server
GEPPETTO.MessageSocket.socket.onerror = function (evt) {
var message = GEPPETTO.Resources.SERVER_CONNECTION_ERROR;
//Attempt to connect using ws first time wss fails,
//if ws fails too then don't try again and display info error window
if (GEPPETTO.MessageSocket.failsafe) {
GEPPETTO.MessageSocket.protocol = "ws://";
GEPPETTO.MessageSocket.failsafe = true;
GEPPETTO.MessageSocket.connect(GEPPETTO.MessageSocket.protocol + window.location.host + '/' + GEPPETTO_CONFIGURATION.contextPath + '/GeppettoServlet');
} else {
GEPPETTO.ModalFactory.infoDialog(GEPPETTO.Resources.WEBSOCKET_CONNECTION_ERROR, message);
}
};
},
/**
* Sends messages to the server
*/
send: function (command, parameter, callback) {
var requestID = this.createRequestID();
//if there's a script running let it know the requestID it's using to send one of it's commands
if (GEPPETTO.ScriptRunner.isScriptRunning()) {
GEPPETTO.ScriptRunner.waitingForServerResponse(requestID);
}
this.waitForConnection(messageTemplate(requestID, command, parameter), connectionInterval);
// add callback with request id if any
if (callback != undefined) {
callbackHandler[requestID] = callback;
}
return requestID;
},
waitForConnection: function (messageTemplate, interval) {
if (this.isReady() === 1) {
GEPPETTO.MessageSocket.socket.send(messageTemplate);
}
else {
var that = this;
setTimeout(function () {
that.waitForConnection(messageTemplate);
}, interval);
}
},
isReady: function () {
return GEPPETTO.MessageSocket.socket.readyState;
},
close: function () {
GEPPETTO.MessageSocket.socket.close();
//dispose of handlers upon closing connection
messageHandlers = [];
},
/**
* Add handler to receive updates from server
*/
addHandler: function (handler) {
messageHandlers.push(handler);
},
/**
* Removes a handler from the socket
*/
removeHandler: function (handler) {
var index = messageHandlers.indexOf(handler);
if (index > -1) {
messageHandlers.splice(index, 1);
}
},
/**
* Clear handlers
*/
clearHandlers: function () {
messageHandlers = [];
},
/**
* Sets the id of the client
*/
setClientID: function (id) {
clientID = id;
},
/**
* Creates a request id to send with the message to the server
*/
createRequestID: function () {
return clientID + "-" + (nextID++);
}
};
/**
* Template for Geppetto message
*
* @param msgtype - message type
* @param payload - message payload, can be anything
* @returns JSON stringified object
*/
function messageTemplate(id, msgtype, payload) {
if (!(typeof payload == 'string' || payload instanceof String)) {
payload = JSON.stringify(payload);
}
var object = {
requestID: id,
type: msgtype,
data: payload
};
return JSON.stringify(object);
}
function gzipUncompress(compressedMessage) {
var messageBytes = new Uint8Array(compressedMessage);
var message = pako.ungzip(messageBytes, {to: "string"});
return message;
}
function parseAndNotify(messageData) {
var parsedServerMessage = JSON.parse(messageData);
//notify all handlers
for (var i = 0, len = messageHandlers.length; i < len; i++) {
var handler = messageHandlers[i];
if (handler != null || handler != undefined) {
handler.onMessage(parsedServerMessage);
}
}
// run callback if any
if(parsedServerMessage.requestID != undefined){
if (callbackHandler[parsedServerMessage.requestID] != undefined) {
callbackHandler[parsedServerMessage.requestID](parsedServerMessage.data);
delete callbackHandler[parsedServerMessage.requestID];
}
}
}
function processBinaryMessage(message) {
var messageBytes = new Uint8Array(message);
// if it's a binary message and first byte it's zero then assume it's a compressed json string
//otherwise is a file and a 'save as' dialog is opened
if (messageBytes[0] == 0) {
var message = pako.ungzip(messageBytes.subarray(1), {to: "string"});
parseAndNotify(message);
}
else {
var fileNameLength = messageBytes[1];
var fileName = String.fromCharCode.apply(null, messageBytes.subarray(2, 2 + fileNameLength));
var blob = new Blob([message]);
FileSaver.saveAs(blob.slice(2 + fileNameLength), fileName);
}
}
}
});