Skip to content

Commit

Permalink
Merge branch 'implement_heartbeat_keepalive_viz' into update_bindings…
Browse files Browse the repository at this point in the history
…_103023
  • Loading branch information
aymanhab committed Oct 30, 2023
2 parents 0d26cf9 + 4736534 commit 3da2fd9
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
package org.eclipse.jetty;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
Expand All @@ -43,6 +47,8 @@
@WebSocket
public class MyWebSocketHandler {

private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
Expand All @@ -55,6 +61,16 @@ public void onError(Throwable t) {

@OnWebSocketConnect
public void onConnect(Session session) {
executorService.scheduleAtFixedRate(() -> {
try {
String data = "Ping";
ByteBuffer payload = ByteBuffer.wrap(data.getBytes());
session.getRemote().sendPing(payload);
} catch (IOException e) {
e.printStackTrace();
}
},
5, 5, TimeUnit.MINUTES);
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello Webbrowser");
Expand Down

0 comments on commit 3da2fd9

Please sign in to comment.