2012-07-26 21:33:00 +09:00
|
|
|
var WebSocketHandler = function(terminal, uri) {
|
|
|
|
|
|
|
|
|
|
this.terminal = terminal;
|
|
|
|
|
this.uri = uri;
|
|
|
|
|
|
|
|
|
|
this.webSocket = new WebSocket(uri);
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
this.onOpen = function(e) {
|
|
|
|
|
self.terminal.ui.println('Connected to ' + self.uri, self.INFO_COLOR);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.onClose = function(e) {
|
|
|
|
|
self.terminal.ui.println('Disconnected from ' + self.uri, self.INFO_COLOR);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.onMessage = function(e) {
|
|
|
|
|
var packet = JSON.parse(e.data);
|
2012-08-01 12:06:58 +09:00
|
|
|
var id = packet.id;
|
|
|
|
|
if (id == null) {
|
|
|
|
|
self.terminal.ui.println('ID is null', self.FAILURE_COLOR);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var callback = self.terminal.callbacks[id.toString()];
|
|
|
|
|
delete self.terminal.callbacks[id.toString()];
|
2012-08-05 19:21:17 +09:00
|
|
|
var args = self.terminal.args[id.toString()];
|
|
|
|
|
delete self.terminal.args[id.toString()];
|
2012-08-01 12:06:58 +09:00
|
|
|
if (callback == null) {
|
|
|
|
|
self.terminal.ui.println('Callback is null for ID ' + id, self.FAILURE_COLOR);
|
|
|
|
|
return;
|
2012-07-26 21:33:00 +09:00
|
|
|
}
|
2012-08-05 19:21:17 +09:00
|
|
|
callback(packet, args);
|
2012-07-26 21:33:00 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.onError = function(e) {
|
|
|
|
|
self.terminal.ui.println('Error on ' + self.uri, self.INFO_COLOR);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.webSocket.onopen = this.onOpen;
|
|
|
|
|
this.webSocket.onclose = this.onClose;
|
|
|
|
|
this.webSocket.onmessage = this.onMessage;
|
|
|
|
|
this.webSocket.onerror = this.onError;
|
|
|
|
|
|
|
|
|
|
};
|
2012-07-30 16:36:21 +09:00
|
|
|
|
|
|
|
|
WebSocketHandler.prototype.INFO_COLOR = '#a080a0';
|
|
|
|
|
WebSocketHandler.prototype.SUCCESS_COLOR = '#4080c0';
|
|
|
|
|
WebSocketHandler.prototype.FAILURE_COLOR = '#f06040';
|