Skip to content

Commit ff36703

Browse files
committed
update with tenantId and keepalive
1 parent 7ab158c commit ff36703

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>websocket-server</artifactId>
13-
<version>1.0.12</version>
13+
<version>1.0.13</version>
1414
<name>WebsocketServer</name>
1515
<packaging>jar</packaging>
1616

@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>info.unterrainer.commons</groupId>
3535
<artifactId>oauth-token-manager</artifactId>
36-
<version>1.0.7</version>
36+
<version>1.0.10</version>
3737
</dependency>
3838
</dependencies>
3939

src/main/java/info/unterrainer/websocketserver/WsOauthHandlerBase.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
import java.io.EOFException;
44
import java.io.IOException;
5+
import java.nio.ByteBuffer;
6+
import java.util.HashMap;
57
import java.util.Set;
68
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.ScheduledExecutorService;
11+
import java.util.concurrent.TimeUnit;
712

813
import org.eclipse.jetty.websocket.api.Session;
914

15+
import info.unterrainer.commons.jreutils.ShutdownHook;
1016
import info.unterrainer.oauthtokenmanager.OauthTokenManager;
1117
import io.javalin.websocket.WsBinaryMessageContext;
1218
import io.javalin.websocket.WsCloseContext;
@@ -21,6 +27,37 @@ public class WsOauthHandlerBase extends WsHandlerBase {
2127
protected OauthTokenManager tokenHandler;
2228
protected Set<WsConnectContext> clientsConnected = ConcurrentHashMap.newKeySet();
2329
protected Set<WsConnectContext> clientsQuarantined = ConcurrentHashMap.newKeySet();
30+
protected HashMap<Session, String> tenantIdsBySession = new HashMap<>();
31+
32+
protected ScheduledExecutorService hb = Executors.newSingleThreadScheduledExecutor(r -> {
33+
Thread t = new Thread(r, "ws-heartbeat");
34+
t.setDaemon(true);
35+
return t;
36+
});
37+
38+
public WsOauthHandlerBase() {
39+
super();
40+
ShutdownHook.register(() -> {
41+
hb.close();
42+
hb = null;
43+
});
44+
45+
hb.scheduleAtFixedRate(() -> {
46+
for (WsConnectContext c : clientsConnected) {
47+
Session s = c.session;
48+
if (s.isOpen()) {
49+
try {
50+
s.getRemote().sendPing(ByteBuffer.allocate(1));
51+
} catch (Exception e) {
52+
try {
53+
s.close(1000, "heartbeat failed");
54+
} catch (Exception ignore) {
55+
}
56+
}
57+
}
58+
}
59+
}, 30, 30, TimeUnit.SECONDS);
60+
}
2461

2562
void setTokenHandler(OauthTokenManager tokenHandler) {
2663
this.tokenHandler = tokenHandler;
@@ -64,7 +101,8 @@ public void onConnect(WsConnectContext ctx) throws Exception {
64101
}
65102
log.debug("New client token: [{}]", token);
66103
try {
67-
tokenHandler.checkAccess(token);
104+
String tenantId = tokenHandler.checkAccess(token);
105+
tenantIdsBySession.put(ctx.session, tenantId);
68106
clientsConnected.add(ctx);
69107
} catch (Exception e) {
70108
log.debug("Token validation failed for client [{}]. Disconnecting.", ctx.session.getRemoteAddress(), e);
@@ -90,7 +128,8 @@ public final void onMessage(WsMessageContext ctx) throws Exception {
90128
return;
91129
}
92130
try {
93-
tokenHandler.checkAccess(ctx.message());
131+
String tenantId = tokenHandler.checkAccess(ctx.message());
132+
tenantIdsBySession.put(ctx.session, tenantId);
94133
WsConnectContext client = getQuarantinedClient(ctx.session);
95134
log.debug("Client [{}] passed token validation. Moving from quarantine to connected.",
96135
ctx.session.getRemoteAddress());

src/test/java/info/unterrainer/websocketserver/AiComm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AiComm extends WsOauthHandlerBase {
99

1010
@Override
1111
public void onMsg(WsMessageContext ctx) throws Exception {
12-
super.onMessage(ctx);
12+
super.onMsg(ctx);
1313

1414
// Broadcast to all connected WS clients.
1515
for (WsConnectContext client : clientsConnected) {

0 commit comments

Comments
 (0)