aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/api/AsyncSocket.java8
-rw-r--r--web/app/controller/Root.js66
2 files changed, 36 insertions, 38 deletions
diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java
index fe20081d3..7de182d7e 100644
--- a/src/org/traccar/api/AsyncSocket.java
+++ b/src/org/traccar/api/AsyncSocket.java
@@ -66,9 +66,11 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U
}
private void sendData(String key, Collection<?> data) {
- JsonObjectBuilder json = Json.createObjectBuilder();
- json.add(key, JsonConverter.arrayToJson(data));
- getRemote().sendString(json.build().toString(), null);
+ if (!data.isEmpty()) {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ json.add(key, JsonConverter.arrayToJson(data));
+ getRemote().sendString(json.build().toString(), null);
+ }
}
}
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]));
+ }
}
- })
- });
+ }
+ };
}
});