@Override public HandshakeBuilder postProcessHandshakeResponseAsServer( ClientHandshake request, ServerHandshakeBuilder response) throws InvalidHandshakeException { response.put("Upgrade", "websocket"); response.put( "Connection", request.getFieldValue("Connection")); // to respond to a Connection keep alives response.setHttpStatusMessage("Switching Protocols"); String seckey = request.getFieldValue("Sec-WebSocket-Key"); if (seckey == null) throw new InvalidHandshakeException("missing Sec-WebSocket-Key"); response.put("Sec-WebSocket-Accept", generateFinalKey(seckey)); return response; }
@Override public HandshakeState acceptHandshakeAsClient(ClientHandshake request, ServerHandshake response) throws InvalidHandshakeException { if (!request.hasFieldValue("Sec-WebSocket-Key") || !response.hasFieldValue("Sec-WebSocket-Accept")) return HandshakeState.NOT_MATCHED; String seckey_answere = response.getFieldValue("Sec-WebSocket-Accept"); String seckey_challenge = request.getFieldValue("Sec-WebSocket-Key"); seckey_challenge = generateFinalKey(seckey_challenge); if (seckey_challenge.equals(seckey_answere)) return HandshakeState.MATCHED; return HandshakeState.NOT_MATCHED; }
@Override public void onOpen(WebSocket conn, ClientHandshake handshake) { this.sendToAll("new connection: " + handshake.getResourceDescriptor()); System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!"); }