Skip to content

Specifing a Sec WebSocket Protocol

Marcel Prestel edited this page Aug 22, 2018 · 4 revisions

Introduction

The |Sec-WebSocket-Protocol| request-header field can be used to indicate what subprotocols (application-level protocols layered over the WebSocket Protocol) are acceptable to the client.

This library supports custom Sec-WebSocket-Protocols.

How to specify a Sec-WebSocket-Protocol

You can specifiy one or more Sec-WebSocket-Protocol(s) by applying them to them to the draft constructor. This works both for the client and the server!

Example

Let's assume you want we have a Sec-WebSocket-Protocol named ocpp2.0, which is supported by the other endpoint.

If you only wanna allow a specific Sec-WebSocket-Protocol and terminate the connection, if this Sec-WebSocket-Protocol is not supported by the other endpoint, you use a constructor like this.

Draft_6455 draft_ocppOnly = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList(new Protocol("ocpp2.0")));

This draft allows only the Sec-WebSocket-Protocol ocpp2.0, if the other endpoint does not support any Sec-WebSocket-Protocol the connection will be automatically closed.

If you also wanna have a fallback or apply more then one Sec-WebSocket-Protocol, you have to use the constructor like this.

ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
protocols.add(new Protocol("ocpp2.0"));
protocols.add(new Protocol(""));
Draft_6455 draft_ocppAndFallBack = new Draft_6455(Collections.<IExtension>emptyList(), protocols);

You can also find this example here.