Ox.App: add web socket option

This commit is contained in:
rolux 2014-05-10 15:08:08 +02:00
parent b7cf611b4a
commit 7f410b6048

View file

@ -16,6 +16,7 @@ Ox.App = function(options) {
var self = { var self = {
options: Ox.extend({ options: Ox.extend({
name: 'App', name: 'App',
socket: '',
timeout: 60000, timeout: 60000,
type: 'POST', type: 'POST',
url: '/api/' url: '/api/'
@ -35,9 +36,30 @@ Ox.App = function(options) {
}); });
}); });
self.options.socket && connectSocket();
//@ localStorage <f> Ox.localStorage instance //@ localStorage <f> Ox.localStorage instance
that.localStorage = Ox.localStorage(self.options.name); that.localStorage = Ox.localStorage(self.options.name);
function connectSocket() {
that.socket = new WebSocket(self.options.socket);
that.socket.onopen = function(event) {
that.triggerEvent('open', event);
};
that.socket.onmessage = function(event) {
var data = JSON.parse(event.data);
that.triggerEvent(data[0], data[1]);
};
that.socket.onerror = function(event) {
that.triggerEvent('error', event);
setTimeout(connectSocket, 1000)
};
that.socket.onclose = function(event) {
that.triggerEvent('close', event);
setTimeout(connectSocket, 1000);
};
}
function getUserData() { function getUserData() {
return { return {
document: {referrer: document.referrer}, document: {referrer: document.referrer},