From the course: WebSocket Programming with Java EE

Unlock the full course today

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

Code the decoder

Code the decoder

From the course: WebSocket Programming with Java EE

Start my 1-month free trial

Code the decoder

- [Instructor] Now let's have a look at how to code the decoder. Load up the class message decoder and you will see that the text subclass of the decoder class has been implemented, and our message type has been specified as a type from which to decode. There are four methods to implement, the innit method, the destroy method, the willCode method, and the decode method. As before, we will leave the innit and destroy methods empty and only implement the decode method. In the code method, we need to write code that constructs a message object from the JSON string that is passed to the method in the text message parameter. For this, we use a JSON reader, which can be created from a static factory method on the JSON class. So let's create the reader now. So we start with the jsonReader object, like so, and then we call on the JSON class, the createReader method, and we pass it a new StringReader with our textMessage inside, like so. The reader is passed an instance of the…

Contents