I have a WebSocket endpoint configured like this in my WebSocketConfigurer implementation
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new ExceptionWebSocketHandlerDecorator(myWebSocketHandler), "/ws/")
.setAllowedOrigins("*")
.addInterceptors(myHandshakeInterceptor);
}
There are some checks in myWebSocketHandler#beforeHandshake and I would like to send an error message to the user in case the server decides to refuse to upgrade the connection. I've tried this
response.setStatusCode(HttpStatus.BAD_GATEWAY);
try {
response.getBody().write("Error".getBytes());
response.getBody().flush();
} catch (IOException e) {
log.error("Error writing response", e);
}
return false;
The status code works, but the body is empty. How can I send it?
EDIT: turns out the problem was that I used Firefox console to check for the response and it didn't show me anything. If I use cURL to make the same request then everything works fine and I see the message I write to the response body!