Skip to content

Commit

Permalink
WebSocket connection keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Jun 30, 2021
1 parent 419cc92 commit 49724e5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/main/java/org/traccar/api/AsyncSocket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 - 2016 Anton Tananaev ([email protected])
* Copyright 2015 - 2021 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,6 +63,11 @@ public void onWebSocketClose(int statusCode, String reason) {
Context.getConnectionManager().removeListener(userId, this);
}

@Override
public void onKeepalive() {
sendData(new HashMap<>());
}

@Override
public void onUpdateDevice(Device device) {
Map<String, Collection<?>> data = new HashMap<>();
Expand All @@ -85,7 +90,7 @@ public void onUpdateEvent(Event event) {
}

private void sendData(Map<String, Collection<?>> data) {
if (!data.isEmpty() && isConnected()) {
if (isConnected()) {
try {
getRemote().sendString(Context.getObjectMapper().writeValueAsString(data), null);
} catch (JsonProcessingException e) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/traccar/database/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public Map<Event, Position> updateDeviceState(long deviceId) {
return result;
}

public synchronized void sendKeepalive() {
for (Set<UpdateListener> 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)) {
Expand Down Expand Up @@ -185,6 +193,7 @@ public synchronized void updateEvent(long userId, Event event) {
}

public interface UpdateListener {
void onKeepalive();
void onUpdateDevice(Device device);
void onUpdatePosition(Position position);
void onUpdateEvent(Event event);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/traccar/schedule/ScheduleManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Anton Tananaev ([email protected])
* Copyright 2020 - 2021 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@ public void start() {
executor = Executors.newSingleThreadScheduledExecutor();

new TaskDeviceInactivityCheck().schedule(executor);
new TaskWebSocketKeepalive().schedule(executor);

}

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/traccar/schedule/TaskWebSocketKeepalive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 Anton Tananaev ([email protected])
*
* 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();
}

}

0 comments on commit 49724e5

Please sign in to comment.