forked from traccar/traccar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -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<>(); | ||
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -27,6 +27,7 @@ public void start() { | |
executor = Executors.newSingleThreadScheduledExecutor(); | ||
|
||
new TaskDeviceInactivityCheck().schedule(executor); | ||
new TaskWebSocketKeepalive().schedule(executor); | ||
|
||
} | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
src/main/java/org/traccar/schedule/TaskWebSocketKeepalive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |