Frequently asked questions - TooTallNate/Java-WebSocket GitHub Wiki
Interesting wiki pages which might help you
- I am having problems with secure websockets (wss). What should I do?
- How can I enable SO_REUSEADDR?
- How can I enable TCP_NODELAY?
- How can I attach custom data to a WebSocket?
- How can I reject a handshake as a server?
- How can I use a Sec-WebSocket-Protocol?
Question overview
- I'm getting the exception
WebsocketNotConnectedException. What should I do? - How can I add additional headers to my WebSocketClient connection?
- How can I add additional headers to my WebSocketServer response?
- How can I get a specific header in my WebSocketClient?
- How can I get a specific header in my WebSocketServer?
Questions
I'm getting the exception WebsocketNotConnectedException. What should I do?
This exception indicates that the connection is not yet established. Due to that reason you are not allowed to send any message to the other endpoint. You can do one of two things to avoid this exception:
- Check, if the connection is open and ready to send messages with
isOpen() - Use the blocking method
connectBlocking(), which is blocking till a connection is established (or an error occurred)
How can I add additional headers to my WebSocketClient connection?
You can add additional headers (like Origin or Cookie) to your handshake, please check out this example here.
How can I add additional headers to my WebSocketServer response?
You can add additional headers (like Access-Control-Allow-Origin) to your handshake, please check out this example here.
How can I get a specific header in my WebSocketClient?
When onOpen is called, the complete handshake received from the server is included in the ServerHandshake argument.
public void onOpen( ServerHandshake handshake ) {
if (!handshake.hasFieldValue( "Server" )) {
return;
}
String server = handshake.getFieldValue( "Server" );
}
How can I get a specific header in my WebSocketServer?
When onOpen is called, the complete handshake received from the client is included in the ClientHandshake argument.
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
if (!handshake.hasFieldValue( "Cookie" )) {
return;
}
String cookie = handshake.getFieldValue( "Cookie" );
}