From f5ab3489c9abdc825dd5297829119813740e32e6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 15 Dec 2015 14:51:57 +1300 Subject: Use web socket instead of long polling --- web/app/controller/Root.js | 66 ++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 35 deletions(-) (limited to 'web/app/controller') diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index cd868d2bc..36781a2e2 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -84,45 +84,41 @@ Ext.define('Traccar.controller.Root', { }, asyncUpdate: function (first) { - Ext.Ajax.request({ - scope: this, - url: '/api/async', - params: { - first: first - }, - callback: Traccar.app.getErrorHandler(this, function (options, success, response) { - var i, deviceStore, positionStore, data, devices, positions, device, position; - if (success) { - deviceStore = Ext.getStore('Devices'); - positionStore = Ext.getStore('LatestPositions'); - data = Ext.decode(response.responseText).data; - devices = data.devices; - positions = data.positions; + var socket = new WebSocket("ws://" + window.location.host + "/api/socket"); - for (i = 0; i < devices.length; i++) { - device = deviceStore.findRecord('id', devices[i].id, 0, false, false, true); - if (device) { - device.set({ - status: devices[i].status, - lastUpdate: devices[i].lastUpdate - }, { - dirty: false - }); - } - } + socket.onmessage = function (event) { + var i, store, data, array, entity; + + data = Ext.decode(event.data); - for (i = 0; i < positions.length; i++) { - position = positionStore.findRecord('deviceId', positions[i].deviceId, 0, false, false, true); - if (position) { - position.set(positions[i]); - } else { - positionStore.add(Ext.create('Traccar.model.Position', positions[i])); - } + if (data.devices) { + array = data.devices; + store = Ext.getStore('Devices'); + for (i = 0; i < array.length; i++) { + entity = store.findRecord('id', array[i].id, 0, false, false, true); + if (entity) { + entity.set({ + status: array[i].status, + lastUpdate: array[i].lastUpdate + }, { + dirty: false + }); } + } + } - this.asyncUpdate(false); + if (data.positions) { + array = data.positions; + store = Ext.getStore('LatestPositions'); + for (i = 0; i < array.length; i++) { + entity = store.findRecord('deviceId', array[i].deviceId, 0, false, false, true); + if (entity) { + entity.set(array[i]); + } else { + store.add(Ext.create('Traccar.model.Position', array[i])); + } } - }) - }); + } + }; } }); -- cgit v1.2.3