For creating WebSocket server I have used Java-WebSocket library. It looks like this, and it is working.
But now I wish to upgrade, so server can have only one active client connection.
Is there some kind of option for doing this, or should I check all connections, and close all, non-first active, connections?
I have tried that like this:
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake)
{
System.out.println("WebSocketServer::onOpen()");
boolean first = true;
Collection<WebSocket> con = this.connections();
synchronized(con) {
for (WebSocket c : con) {
if (first && c.isOpen()) {
first = false;
} else {
c.close();
}
}
}
}
But this closes all connections, and kills Websocket server. What am I doing wrong?