From 7f410b604855f085f371d37c1635048942f0e1d8 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 10 May 2014 15:08:08 +0200 Subject: [PATCH] Ox.App: add web socket option --- source/Ox.UI/js/Core/App.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/Ox.UI/js/Core/App.js b/source/Ox.UI/js/Core/App.js index d00eefc8..cbe32bc0 100644 --- a/source/Ox.UI/js/Core/App.js +++ b/source/Ox.UI/js/Core/App.js @@ -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 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},