Skip to content

Rejecting a handshake

Marcel Prestel edited this page Sep 11, 2017 · 2 revisions

Introduction

It is possible to reject the handshake as a server to make sure specific additional values are set in the handshake.

Reject a handshake

If you want to reject a handshake, you have to override the method onWebsocketHandshakeReceivedAsServer() in your WebSocketServer class.

In this method you have to throw an InvalidDataException to indicate that this handshake should be rejected.

@Override
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException {
	ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
	//To your checks and throw an InvalidDataException to indicate that you reject this handshake.
	return builder;
}

After you throw the InvalidDataException the server will send a http code 404 to the client resulting in a close.

You can also find an example on how to reject a handshake in the examples folder. The class is called ServerRejectHandshakeExample