From the course: WebSocket Programming with Java EE

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Encoders and decoders

Encoders and decoders

From the course: WebSocket Programming with Java EE

Start my 1-month free trial

Encoders and decoders

- [Narrator] Endcoders and decoders allow custom Java types to be used to represent messages from and to clients. An encoder translates a Java custom type into a form that can be transmitted as a WebSocket message and a decoder performs the reverse function. To create an encoder, you must implement one of these interfaces, depending on whether the WebSocket message should be text or binary. There are equivalent interfaces for TextStream and BinaryStream. For a Java custom type, you would choose to implement the text subclass like so and in doing so, you specify the generic type of the custom Java type. In this case, the custom type is message. You are then required to implement three methods. The init method is used to make configurations to the endpoint for the endpoint of this coder will be used with. And the destroy method is called when the encoder is about to be destroyed and is used to close any resources that the encoder uses. Now, the encode method is where all the fun…

Contents