diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-15 14:51:57 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-15 14:51:57 +1300 |
commit | f5ab3489c9abdc825dd5297829119813740e32e6 (patch) | |
tree | 62d872377f9a2125b5b5dd097c75a2acaedf41dd /web/app/controller | |
parent | 44d38a8f121304fc6504ebaabbfb0a402779ca1d (diff) | |
download | traccar-server-f5ab3489c9abdc825dd5297829119813740e32e6.tar.gz traccar-server-f5ab3489c9abdc825dd5297829119813740e32e6.tar.bz2 traccar-server-f5ab3489c9abdc825dd5297829119813740e32e6.zip |
Use web socket instead of long polling
Diffstat (limited to 'web/app/controller')
-rw-r--r-- | web/app/controller/Root.js | 66 |
1 files changed, 31 insertions, 35 deletions
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])); + } } - }) - }); + } + }; } }); |