I'm trying to send a custom header during the handshake. I can intercept the handshake with the ServerEndpointConfig.Configurator and override modifyHandshake.
public class WebsocketConfigurator extends ServerEndpointConfig.Configurator {
public static final String HEADER_SOCKET_ID = "X-Socket-Id";
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
String id = UUID.randomUUID().toString();
List<String> list = new ArrayList<String>();
list.add(id);
response.getHeaders().put(HEADER_SOCKET_ID, list);
sec.getUserProperties().put(HEADER_SOCKET_ID, id); }
}
The method gets called and the id gets added to the response headers, though it doesn't send the additional headers.
I sniffed it with wireshark to make sure it's not the client framework:
GET /websocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8080
Origin: http://localhost:8080
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: QgZwJwVv8+i/vaKFHDqPZg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36
HTTP/1.1 101 Switching Protocols
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: Kim6Qjj7sOBLfG+6I++gS/OVx4A=
Sec-WebSocket-Extensions: permessage-deflate
.u.....0....y.R...K'6...9M
.....PU.......}..).B..g.P....D..4."!..R|._.@..Fp..G....c..0...].D...F.H...{E-.k{.pP...S{n...
I want to send a generated id to the client and don't want to do it in an extra call. Is there a reason why it's not sent or is there a different approach?
I found an example on the dev blog which also adds a header in modifyHandshake
so I guess theoretically it should be possible.