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 = {
options: Ox.extend({
name: 'App',
socket: '',
timeout: 60000,
type: 'POST',
url: '/api/'
@ -35,9 +36,30 @@ Ox.App = function(options) {
});
});
self.options.socket && connectSocket();
//@ localStorage <f> Ox.localStorage instance
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() {
return {
document: {referrer: document.referrer},