Undertow WebSocket: make sure onConnect is triggered before on message
@@ -31,6 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
public class UtowWebSocket extends AbstractReceiveListener
implements WebSocketConfigurer, WebSocket, WebSocketCallback<Void> {
@@ -45,6 +46,7 @@ public class UtowWebSocket extends AbstractReceiveListener
private OnClose onCloseCallback;
private OnError onErrorCallback;
private String key;
+ private AtomicBoolean initialized = new AtomicBoolean();
public UtowWebSocket(UtowContext ctx, WebSocketChannel channel) {
this.ctx = ctx;
@@ -95,7 +97,8 @@ public class UtowWebSocket extends AbstractReceiveListener
onError(channel, x);
}
} else {
- onError(channel, new IllegalStateException("Attempt to send a message on closed web socket"));
+ onError(channel,
+ new IllegalStateException("Attempt to send a message on closed web socket"));
}
}
return this;
@@ -142,6 +145,8 @@ public class UtowWebSocket extends AbstractReceiveListener
}
void fireConnect() {
+ // fire only once
+ if (initialized.compareAndSet(false, true)) {
try {
addSession(this);
Config conf = ctx.getRouter().getConfig();
@@ -160,9 +165,11 @@ public class UtowWebSocket extends AbstractReceiveListener
onError(channel, x);
}
}
+ }
@Override protected void onFullTextMessage(WebSocketChannel channel,
BufferedTextMessage message) throws IOException {
+ fireConnect();
if (onMessageCallback != null) {
try {
onMessageCallback.onMessage(this, WebSocketMessage.create(getContext(), message.getData()));
