From 49724e51630410e9a2c99a27ec7963a315f6575e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 29 Jun 2021 20:51:13 -0700 Subject: WebSocket connection keepalive --- src/main/java/org/traccar/api/AsyncSocket.java | 9 ++++-- .../org/traccar/database/ConnectionManager.java | 9 ++++++ .../java/org/traccar/schedule/ScheduleManager.java | 3 +- .../traccar/schedule/TaskWebSocketKeepalive.java | 36 ++++++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/traccar/schedule/TaskWebSocketKeepalive.java (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/api/AsyncSocket.java b/src/main/java/org/traccar/api/AsyncSocket.java index b2ff5031a..b1853822d 100644 --- a/src/main/java/org/traccar/api/AsyncSocket.java +++ b/src/main/java/org/traccar/api/AsyncSocket.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2021 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,6 +63,11 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U Context.getConnectionManager().removeListener(userId, this); } + @Override + public void onKeepalive() { + sendData(new HashMap<>()); + } + @Override public void onUpdateDevice(Device device) { Map> data = new HashMap<>(); @@ -85,7 +90,7 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U } private void sendData(Map> data) { - if (!data.isEmpty() && isConnected()) { + if (isConnected()) { try { getRemote().sendString(Context.getObjectMapper().writeValueAsString(data), null); } catch (JsonProcessingException e) { diff --git a/src/main/java/org/traccar/database/ConnectionManager.java b/src/main/java/org/traccar/database/ConnectionManager.java index 5ff27c187..e12d44612 100644 --- a/src/main/java/org/traccar/database/ConnectionManager.java +++ b/src/main/java/org/traccar/database/ConnectionManager.java @@ -154,6 +154,14 @@ public class ConnectionManager { return result; } + public synchronized void sendKeepalive() { + for (Set userListeners : listeners.values()) { + for (UpdateListener listener : userListeners) { + listener.onKeepalive(); + } + } + } + public synchronized void updateDevice(Device device) { for (long userId : Context.getPermissionsManager().getDeviceUsers(device.getId())) { if (listeners.containsKey(userId)) { @@ -185,6 +193,7 @@ public class ConnectionManager { } public interface UpdateListener { + void onKeepalive(); void onUpdateDevice(Device device); void onUpdatePosition(Position position); void onUpdateEvent(Event event); diff --git a/src/main/java/org/traccar/schedule/ScheduleManager.java b/src/main/java/org/traccar/schedule/ScheduleManager.java index 4def211d0..5d5054100 100644 --- a/src/main/java/org/traccar/schedule/ScheduleManager.java +++ b/src/main/java/org/traccar/schedule/ScheduleManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 2021 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ public class ScheduleManager { executor = Executors.newSingleThreadScheduledExecutor(); new TaskDeviceInactivityCheck().schedule(executor); + new TaskWebSocketKeepalive().schedule(executor); } diff --git a/src/main/java/org/traccar/schedule/TaskWebSocketKeepalive.java b/src/main/java/org/traccar/schedule/TaskWebSocketKeepalive.java new file mode 100644 index 000000000..953b0efea --- /dev/null +++ b/src/main/java/org/traccar/schedule/TaskWebSocketKeepalive.java @@ -0,0 +1,36 @@ +/* + * Copyright 2021 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.schedule; + +import org.traccar.Context; + +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class TaskWebSocketKeepalive implements Runnable { + + private static final long PERIOD_SECONDS = 55; + + public void schedule(ScheduledExecutorService executor) { + executor.scheduleAtFixedRate(this, PERIOD_SECONDS, PERIOD_SECONDS, TimeUnit.SECONDS); + } + + @Override + public void run() { + Context.getConnectionManager().sendKeepalive(); + } + +} -- cgit v1.2.3