aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/api/AsyncSocket.java58
-rw-r--r--src/org/traccar/api/AsyncSocketServlet.java12
2 files changed, 62 insertions, 8 deletions
diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java
index 8630b207d..fe20081d3 100644
--- a/src/org/traccar/api/AsyncSocket.java
+++ b/src/org/traccar/api/AsyncSocket.java
@@ -16,15 +16,59 @@
package org.traccar.api;
import org.eclipse.jetty.websocket.api.Session;
-import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
-import org.eclipse.jetty.websocket.api.annotations.WebSocket;
+import org.eclipse.jetty.websocket.api.WebSocketAdapter;
+import org.traccar.Context;
+import org.traccar.database.ConnectionManager;
+import org.traccar.model.Device;
+import org.traccar.model.Position;
+import org.traccar.web.JsonConverter;
-@WebSocket
-public class AsyncSocket {
+import javax.json.Json;
+import javax.json.JsonObjectBuilder;
+import java.util.Arrays;
+import java.util.Collection;
- @OnWebSocketMessage
- public void onWebSocketText(Session session, String message) {
- session.getRemote().sendString(message, null);
+public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.UpdateListener {
+
+ private static final String KEY_DEVICES = "devices";
+ private static final String KEY_POSITIONS = "positions";
+
+ private Collection<Long> devices;
+
+ public AsyncSocket(long userId) {
+ devices = Context.getPermissionsManager().allowedDevices(userId);
+ }
+
+ @Override
+ public void onWebSocketConnect(Session session) {
+ super.onWebSocketConnect(session);
+
+ sendData(KEY_POSITIONS, Context.getConnectionManager().getInitialState(devices));
+
+ Context.getConnectionManager().addListener(devices, this);
+ }
+
+ @Override
+ public void onWebSocketClose(int statusCode, String reason) {
+ super.onWebSocketClose(statusCode, reason);
+
+ Context.getConnectionManager().removeListener(devices, this);
+ }
+
+ @Override
+ public void onUpdateDevice(Device device) {
+ sendData(KEY_DEVICES, Arrays.asList(device));
+ }
+
+ @Override
+ public void onUpdatePosition(Position position) {
+ sendData(KEY_POSITIONS, Arrays.asList(position));
+ }
+
+ private void sendData(String key, Collection<?> data) {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ json.add(key, JsonConverter.arrayToJson(data));
+ getRemote().sendString(json.build().toString(), null);
}
}
diff --git a/src/org/traccar/api/AsyncSocketServlet.java b/src/org/traccar/api/AsyncSocketServlet.java
index 3b02bcfcd..59f0b7ee2 100644
--- a/src/org/traccar/api/AsyncSocketServlet.java
+++ b/src/org/traccar/api/AsyncSocketServlet.java
@@ -15,8 +15,12 @@
*/
package org.traccar.api;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
+import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
+import org.traccar.api.resource.SessionResource;
public class AsyncSocketServlet extends WebSocketServlet {
@@ -25,7 +29,13 @@ public class AsyncSocketServlet extends WebSocketServlet {
@Override
public void configure(WebSocketServletFactory factory) {
factory.getPolicy().setIdleTimeout(ASYNC_TIMEOUT);
- factory.register(AsyncSocket.class);
+ factory.setCreator(new WebSocketCreator() {
+ @Override
+ public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
+ long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY);
+ return new AsyncSocket(userId);
+ }
+ });
}
}