Moved sockets. Fixed sockets.

This commit is contained in:
Alexander Kastius
2016-08-17 17:25:27 +02:00
parent 97c1c5e455
commit aec7c593d8
6 changed files with 147 additions and 106 deletions

View File

@@ -1,89 +1,90 @@
CodeOceanEditorWebsocket = {
initWebsocketConnection: function (url, onmessageFunction) {
//TODO: get the protocol from config file dependent on environment. (dev: ws, prod: wss)
//causes: Puma::HttpParserError: Invalid HTTP format, parsing fails.
//TODO: make sure that this gets cached.
this.websocket = new WebSocket('<%= DockerClient.config['ws_client_protocol'] %>' + window.location.hostname + ':' + window.location.port + url);
this.websocket.onopen = function (evt) {
this.resetOutputTab();
}.bind(this); // todo show some kind of indicator for established connection
this.websocket.onclose = function (evt) { /* expected at some point */
}.bind(this);
this.websocket.onmessage = onmessageFunction;
this.websocket.onerror = function (evt) {
this.showWebsocketError();
}.bind(this);
this.websocket.flush = function () {
this.send('\n');
}
},
//ToDo: Move websocket and commands variable in here
executeWebsocketCommand: function (msg) {
if ($.inArray(msg.cmd, this.commands) == -1) {
console.log("Unknown command: " + msg.cmd);
// skipping unregistered commands is required
// as we may receive mirrored response due to internal behaviour
return;
}
switch (msg.cmd) {
case 'input':
this.showPrompt(msg);
break;
case 'write':
this.printWebsocketOutput(msg);
break;
case 'turtle':
this.initTurtle();
this.showCanvas();
this.handleTurtleCommand(msg);
break;
case 'turtlebatch':
this.initTurtle();
this.showCanvas();
this.handleTurtlebatchCommand(msg);
break;
case 'render':
this.renderWebsocketOutput(msg);
break;
case 'exit':
this.killWebsocketAndContainer();
this.handleQaApiOutput();
this.handleStderrOutputForFlowr();
this.augmentStacktraceInOutput();
this.cleanUpTurtle();
break;
case 'timeout':
// just show the timeout message here. Another exit command is sent by the rails backend when the socket to the docker container closes.
this.showTimeoutMessage();
break;
case 'status':
this.showStatus(msg);
break;
}
},
parseCanvasMessage: function (message, recursive) {
var msg;
message = message.replace(/^\s+|\s+$/g, "");
try {
// todo validate json instead of catching
msg = JSON.parse(message);
} catch (e) {
if (!recursive) {
return false;
}
// why does docker sometimes send multiple commands at once?
message = message.replace(/^\s+|\s+$/g, "");
var messages = message.split("\n");
for (var i = 0; i < messages.length; i++) {
if (!messages[i]) {
continue;
}
this.parseCanvasMessage(messages[i], false);
}
return;
}
this.executeWebsocketCommand(msg);
CommandSocket = function(url, onOpen) {
this.handlers = {};
this.websocket = new WebSocket(url);
this.websocket.onopen = onOpen;
this.websocket.onmessage = this.onMessage.bind(this);
this.websocket.flush = function () {
this.send('\n');
}
};
};
CommandSocket.prototype.onError = function(callback){
this.websocket.onerror = callback
};
/**
* Allows it to register an event-handler on the given cmd.
* The handler needs to accept one argument, the message.
* There is only handler per command at the moment.
* @param command
* @param handler
*/
CommandSocket.prototype.on = function(command, handler) {
this.handlers[command] = handler;
};
/**
* Used to initialize the recursive message parser.
* @param event
*/
CommandSocket.prototype.onMessage = function(event) {
//Parses the message (serches for linebreaks) and executes every contained cmd.
this.parseMessage(event.data, true)
};
/**
* Parses a message, checks wether it contains multiple commands (seperated by linebreaks)
* This needs to be done because of the behavior of the docker-socket connection.
* Because of this, sometimes multiple commands might be executed in one message.
* @param message
* @param recursive
* @returns {boolean}
*/
CommandSocket.prototype.parseMessage = function(message, recursive) {
var msg;
var message_string = message.replace(/^\s+|\s+$/g, "");
try {
// todo validate json instead of catching
msg = JSON.parse(message_string);
} catch (e) {
if (!recursive) {
return false;
}
// why does docker sometimes send multiple commands at once?
message_string = message_string.replace(/^\s+|\s+$/g, "");
var messages = message_string.split("\n");
for (var i = 0; i < messages.length; i++) {
if (!messages[i]) {
continue;
}
this.parseMessage(messages[i], false);
}
return;
}
this.executeCommand(msg);
};
/**
* Executes the handler that is registered for a certain command.
* Does nothing if the command was not specified yet.
* If there is a null-handler (defined with on('default',func)) this gets
* executed if the command was not registered or the message has no cmd prop.
* @param cmd
*/
CommandSocket.prototype.executeCommand = function(cmd) {
if ('cmd' in cmd && cmd.cmd in this.handlers) {
this.handlers[cmd.cmd](cmd);
} else if ('default' in this.handlers) {
this.handlers['default'](cmd);
}
};
/**
* Used to send a message through the socket.
* If data is not a string we'll try use jsonify to make it a string.
* @param data
*/
CommandSocket.prototype.send = function(data) {
this.websocket.send(data);
};