Expected Behavior
Closing the websocket connection in onOpen should be possible.
Current Behavior
Calling conn.close() in public void WebSocketServer::onOpen(WebSocket conn, ClientHandshake handshake) leads to a NullPointerException. The socket is still closed correctly, but an error is logged.
Steps to Reproduce (for bugs)
- Run a server which closes new connections right away in onOpen like below.
- Try to connect with any client.
import java.net.InetSocketAddress;
import org.java_websocket.WebSocket;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
public class TestWebSocketServer extends WebSocketServer {
public TestWebSocketServer(int port) {
super(new InetSocketAddress(port));
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.close(CloseFrame.POLICY_VALIDATION, "Not authorized");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}
@Override
public void onMessage(WebSocket conn, String message) {
}
@Override
public void onError(WebSocket conn, Exception ex) {
}
@Override
public void onStart() {
}
public static void main(String[] args) {
TestWebSocketServer server = new TestWebSocketServer(9999);
server.start();
}
}
Debug log (for bugs)
Error while reading from remote connection: java.lang.NullPointerException
java.lang.NullPointerException
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:214)
at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:881)
Context
I'm trying to implement an authentication scheme for my websocket service using a token which gets passed to the websocket server in a cookie during the handshake. Rejecting the handshake in onWebsocketHandshakeReceivedAsServer isn't an option for me, because I can't destinguish between the server rejecting the connection and the server being offline in the browser.
I need to wait until the connection is open to validate the token and send a custom closecode if it's invalid.
Your Environment
- Version used: 1.3.6
- Java version: 1.8.0_152
- Operating System and version: Windows 10 x64
Expected Behavior
Closing the websocket connection in onOpen should be possible.
Current Behavior
Calling
conn.close()inpublic void WebSocketServer::onOpen(WebSocket conn, ClientHandshake handshake)leads to a NullPointerException. The socket is still closed correctly, but an error is logged.Steps to Reproduce (for bugs)
Debug log (for bugs)
Context
I'm trying to implement an authentication scheme for my websocket service using a token which gets passed to the websocket server in a cookie during the handshake. Rejecting the handshake in
onWebsocketHandshakeReceivedAsServerisn't an option for me, because I can't destinguish between the server rejecting the connection and the server being offline in the browser.I need to wait until the connection is open to validate the token and send a custom
closecodeif it's invalid.Your Environment