Skip to content

Commit ec8dd26

Browse files
committed
avoid duplicate connecting event, better Exception msg if tokenGetter not set
1 parent 27374a9 commit ec8dd26

File tree

2 files changed

+21
-11
lines changed
  • centrifuge/src/main/java/io/github/centrifugal/centrifuge
  • example/src/main/java/io/github/centrifugal/centrifuge/example

2 files changed

+21
-11
lines changed

centrifuge/src/main/java/io/github/centrifugal/centrifuge/Client.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,12 @@ void processDisconnect(int code, String reason, Boolean shouldReconnect) {
185185
this.reconnectTask.cancel(true);
186186
this.reconnectTask = null;
187187
}
188+
boolean needEvent;
188189
if (shouldReconnect) {
190+
needEvent = previousState != ClientState.CONNECTING;
189191
this.setState(ClientState.CONNECTING);
190192
} else {
193+
needEvent = previousState != ClientState.DISCONNECTED;
191194
this.setState(ClientState.DISCONNECTED);
192195
}
193196

@@ -212,12 +215,14 @@ void processDisconnect(int code, String reason, Boolean shouldReconnect) {
212215
}
213216
}
214217

215-
if (shouldReconnect) {
216-
ConnectingEvent event = new ConnectingEvent(code, reason);
217-
this.listener.onConnecting(this, event);
218-
} else {
219-
DisconnectedEvent event = new DisconnectedEvent(code, reason);
220-
this.listener.onDisconnected(this, event);
218+
if (needEvent) {
219+
if (shouldReconnect) {
220+
ConnectingEvent event = new ConnectingEvent(code, reason);
221+
this.listener.onConnecting(this, event);
222+
} else {
223+
DisconnectedEvent event = new DisconnectedEvent(code, reason);
224+
this.listener.onDisconnected(this, event);
225+
}
221226
}
222227

223228
this.ws.close(NORMAL_CLOSURE_STATUS, "");
@@ -335,12 +340,15 @@ public void onFailure(WebSocket webSocket, Throwable t, Response response) {
335340
});
336341
}
337342

338-
private void handleConnectionOpen() {
343+
private void handleConnectionOpen() throws Exception {
339344
if (this.getState() != ClientState.CONNECTING) {
340345
return;
341346
}
342347
if (this.refreshRequired || (this.token == null && this.opts.getTokenGetter() != null)) {
343348
ConnectionTokenEvent connectionTokenEvent = new ConnectionTokenEvent();
349+
if (this.opts.getTokenGetter() == null) {
350+
throw new Exception("tokenGetter function should be provided in Client options to handle token refresh, see Options.setTokenGetter");
351+
}
344352
this.opts.getTokenGetter().getConnectionToken(connectionTokenEvent, (err, token) -> this.executor.submit(() -> {
345353
if (Client.this.getState() != ClientState.CONNECTING) {
346354
return;
@@ -597,6 +605,7 @@ private void handleConnectReply(Protocol.Reply reply) {
597605
sub.resubscribeIfNecessary();
598606
}
599607
}
608+
600609
for (Map.Entry<String, Protocol.SubscribeResult> entry : result.getSubsMap().entrySet()) {
601610
Protocol.SubscribeResult subResult = entry.getValue();
602611
String channel = entry.getKey();
@@ -656,6 +665,7 @@ private void handleConnectReply(Protocol.Reply reply) {
656665
}
657666
}
658667
}
668+
659669
this.connectCommands.clear();
660670

661671
for (Map.Entry<Integer, Protocol.Command> entry : this.connectAsyncCommands.entrySet()) {

example/src/main/java/io/github/centrifugal/centrifuge/example/Main.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public void onConnecting(Client client, ConnectingEvent event) {
5050
}
5151
@Override
5252
public void onDisconnected(Client client, DisconnectedEvent event) {
53-
System.out.printf("disconnected %d %s", event.getCode(), event.getReason());
53+
System.out.printf("disconnected %d %s%n", event.getCode(), event.getReason());
5454
}
5555
@Override
5656
public void onError(Client client, ErrorEvent event) {
57-
System.out.println("There was a problem connecting!");
57+
System.out.printf("There was a problem connecting: %s%n", event.getError().toString());
5858
}
5959
@Override
6060
public void onMessage(Client client, MessageEvent event) {
@@ -89,7 +89,7 @@ public void onLeave(Client client, ServerLeaveEvent event) {
8989
};
9090

9191
Options opts = new Options();
92-
// opts.setToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0c3VpdGVfand0In0.hPmHsVqvtY88PvK4EmJlcdwNuKFuy3BGaF7dMaKdPlw");
92+
// opts.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyMSIsImV4cCI6MTY1OTQ0MTY4MiwiaWF0IjoxNjU4ODM2ODgyfQ.tyd2_TVq29WZuhh5OBni6dq7Lqry2s8z2PHZavplr7A");
9393
// opts.setConnectionTokenGetter(new ConnectionTokenGetter() {
9494
// @Override
9595
// public void getConnectionToken(ConnectionTokenEvent event, TokenCallback cb) {
@@ -106,7 +106,7 @@ public void onLeave(Client client, ServerLeaveEvent event) {
106106
// });
107107

108108
Client client = new Client(
109-
"ws://localhost:8000/connection/websocket?cf_protocol=protobuf",
109+
"ws://localhost:8000/connection/websocket",
110110
opts,
111111
listener
112112
);

0 commit comments

Comments
 (0)