|
|
@ -60,8 +60,15 @@ ConnectionHandler.prototype.onopen = function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionHandler.prototype.onclose = function(event) {
|
|
|
|
ConnectionHandler.prototype.onclose = function(event) {
|
|
|
|
console.log({close: event});
|
|
|
|
console.log({close: event});
|
|
|
|
|
|
|
|
this.broadcastClose();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionHandler.prototype.broadcastClose = function() {
|
|
|
|
|
|
|
|
var event = document.createEvent("Event");
|
|
|
|
|
|
|
|
event.initEvent("wsClosed", true, true);
|
|
|
|
|
|
|
|
document.dispatchEvent(event);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionHandler.prototype.onmessage = function(event) {
|
|
|
|
ConnectionHandler.prototype.onmessage = function(event) {
|
|
|
|
this.broadcastMessageReceived(event.data);
|
|
|
|
this.broadcastMessageReceived(event.data);
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -85,36 +92,57 @@ ConnectionHandler.prototype.sendMsg = function(message) {
|
|
|
|
// response rendering
|
|
|
|
// response rendering
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
var MessageDisplay = function(selector, templateSelector, errorTemplateSelector) {
|
|
|
|
var MessageDisplay = function(selector, requestTemplateSelector, responseTemplateSelector, errorTemplateSelector, sysTemplateSelector) {
|
|
|
|
this.elem = $(selector);
|
|
|
|
this.elem = $(selector);
|
|
|
|
this.renderMessage = _.template($(templateSelector).html());
|
|
|
|
this.renderRequest = _.template($(requestTemplateSelector).html());
|
|
|
|
|
|
|
|
this.renderResponse = _.template($(responseTemplateSelector).html());
|
|
|
|
this.renderError = _.template($(errorTemplateSelector).html());
|
|
|
|
this.renderError = _.template($(errorTemplateSelector).html());
|
|
|
|
|
|
|
|
this.renderSys = _.template($(sysTemplateSelector).html());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MessageDisplay.prototype.addMessage = function(rawmessage) {
|
|
|
|
MessageDisplay.prototype.addRequest = function(request) {
|
|
|
|
var message = JSON.parse(rawmessage);
|
|
|
|
this.elem.append(this.renderRequest({message: request}));
|
|
|
|
if (message.is_error) {
|
|
|
|
};
|
|
|
|
var rendered = this.renderError(message);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
MessageDisplay.prototype.addResponse = function(response) {
|
|
|
|
var rendered = this.renderMessage(message);
|
|
|
|
this.elem.append(this.renderResponse({message: response}));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.elem.append(rendered);
|
|
|
|
|
|
|
|
|
|
|
|
MessageDisplay.prototype.addError = function(error) {
|
|
|
|
|
|
|
|
this.elem.append(this.renderError({message: error}));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MessageDisplay.prototype.addSys = function(sys) {
|
|
|
|
|
|
|
|
this.elem.append(this.renderSys({message: sys}));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var Skeam = function(config) {
|
|
|
|
var Skeam = function(config) {
|
|
|
|
this.inputHandler = new InputHandler(config.inputSelector);
|
|
|
|
this.inputHandler = new InputHandler(config.inputSelector);
|
|
|
|
this.messageDisplay = new MessageDisplay(config.outputSelector,
|
|
|
|
this.messageDisplay = new MessageDisplay(config.outputSelector,
|
|
|
|
config.messageTemplateSelector,
|
|
|
|
config.requestTemplateSelector,
|
|
|
|
config.errorTemplateSelector);
|
|
|
|
config.responseTemplateSelector,
|
|
|
|
|
|
|
|
config.errorTemplateSelector,
|
|
|
|
|
|
|
|
config.sysTemplateSelector);
|
|
|
|
this.conn = new ConnectionHandler(config.wsPath);
|
|
|
|
this.conn = new ConnectionHandler(config.wsPath);
|
|
|
|
document.addEventListener("sendMsg", _.bind(this.sendMsg, this), false);
|
|
|
|
document.addEventListener("sendMsg", _.bind(this.sendMsg, this), false);
|
|
|
|
document.addEventListener("receiveResponse", _.bind(this.receiveResponse, this), false);
|
|
|
|
document.addEventListener("receiveResponse", _.bind(this.receiveResponse, this), false);
|
|
|
|
|
|
|
|
document.addEventListener("wsClosed", _.bind(this.onclose, this), false);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Skeam.prototype.sendMsg = function(event) {
|
|
|
|
Skeam.prototype.sendMsg = function(event) {
|
|
|
|
|
|
|
|
this.messageDisplay.addRequest(event.message);
|
|
|
|
this.conn.sendMsg(event.message + "\n");
|
|
|
|
this.conn.sendMsg(event.message + "\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Skeam.prototype.receiveResponse = function(event) {
|
|
|
|
Skeam.prototype.receiveResponse = function(event) {
|
|
|
|
this.messageDisplay.addMessage(event.message);
|
|
|
|
var message = JSON.parse(event.message);
|
|
|
|
}
|
|
|
|
if (message.is_error) {
|
|
|
|
|
|
|
|
this.messageDisplay.addError(message.message);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.messageDisplay.addResponse(message.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Skeam.prototype.onclose = function() {
|
|
|
|
|
|
|
|
this.messageDisplay.addSys("connection closed");
|
|
|
|
|
|
|
|
};
|
|
|
|