From the course: WebSocket Programming with Java EE

Code the encoder

From the course: WebSocket Programming with Java EE

Start my 1-month free trial

Code the encoder

- [Instructor] Okay. So let's code the Encoder. You should open the Message and go to class. I have it already open here on the screen. And as you can see, it implements the Text subclass of the Encoder class, and specifies the generic type to be our Message class. Okay, so let's have a quick look at this Message class. As you can see, this is a very simple class with just three fields. The first field called content will contain the actual text message itself, while the sender field represents the user's name, and the received field is the time the message was actually received. Okay, so let's go back to the Encoder class. Now there are three methods to be implemented. The init method, the destroy method, and the encode method. However, in this application, we will only implement the encode method. Now this is because there are no initialization configurations to be done, so no code is needed in init method. Nor are we going to use a resource that needs to be closed on application shutdown, so no code is necessary in the destroy method. In the encode method, we're going to write code that creates a JSON string from the Message object. Encoding happens when the message is sent from the server to a client, and we're going to use JSONP to create the JSON string representation from the Message object. Now, if you remember, we talked about using the JSONP object model to construct a JSON object instance using the JSON builder. Well, we're going to do exactly that in this example. So let's start with the JSON builder. So we start with the JSON builder by calling a static builder method on the JSON class. And the method is called createObjectBuilder. This now creates an empty JSON object. Now we use the add method to add a property and value to the JSON object. So add, we'll pass it a key name, which is going to be content. And then we're gonna pass it the value of content. And we get that by calling the getContent method on our message instance. So as you can see here, what we have done is to add a property named content and its corresponding value by calling the getter method of our Message object. We now do the same for the other two properties. So add, the next property is going to be sender, and we get its value by calling getSender. And now we do the same for received, and we tall, and we call the getReceived method to get the value from the message. Now to build the JSON object, we call the build method. So we simply call the build method like so. And finally, we need to get the string representation of the JSON object by calling the toString method. So finally we call our toString method. And all we need to do now is to return this string value. And we do it by simply doing return here and removing this. And that's it, we now have a properly formed JSON string representation of the message. Now we can send over a websocket connection to the client.

Contents