Developing a Chat Application with WebSocket Endpoints | Java EE 7 Exam Preparation

WebSocket Endpoints for Displaying Emoticons and Images in Chat Application

Question

You need to develop a chat application, which allows the display of emoticons and images together with text messages.

How should you configure a WebSocket endpoints to receive both text and binary messages?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

To receive both text and binary messages in a WebSocket endpoint, you can use either of the following approaches:

Approach 1: Create two @onMessage methods in the same endpoint with appropriate parameter types.

This approach involves creating two separate @OnMessage methods in the same WebSocket endpoint, one to handle text messages and another to handle binary messages. You can specify the parameter types of the two methods to match the message types you expect to receive.

Here's an example of how you can implement this approach:

java
@ServerEndpoint("/chat") public class ChatEndpoint { @OnMessage public void onTextMessage(String message) { // handle text message } @OnMessage public void onBinaryMessage(ByteBuffer buffer) { // handle binary message } }

In the above code, the onTextMessage method handles text messages and takes a String parameter, while the onBinaryMessage method handles binary messages and takes a ByteBuffer parameter.

Approach 2: Create two @onMessage methods, each with appropriate decoder attribute in the same endpoint.

This approach involves creating two separate @OnMessage methods in the same WebSocket endpoint, but with each method specifying a different decoder attribute. This allows the WebSocket container to automatically decode the incoming message based on the specified decoder, without you having to manually check the message type.

Here's an example of how you can implement this approach:

java
@ServerEndpoint("/chat") public class ChatEndpoint { @OnMessage(decoders = TextDecoder.class) public void onTextMessage(String message) { // handle text message } @OnMessage(decoders = BinaryDecoder.class) public void onBinaryMessage(byte[] data) { // handle binary message } }

In the above code, the onTextMessage method handles text messages and specifies the TextDecoder class as the decoder to use. The onBinaryMessage method handles binary messages and specifies the BinaryDecoder class as the decoder to use.

Note: The TextDecoder and BinaryDecoder classes are custom decoder classes that you need to implement based on your application's requirements.

Approach 3: Define the @onMessage methods in your endpoint with Object as parameter and check the actual type in your code.

This approach involves defining a single @OnMessage method with an Object parameter, which allows you to handle both text and binary messages. However, you need to manually check the actual type of the incoming message in your code and handle it accordingly.

Here's an example of how you can implement this approach:

java
@ServerEndpoint("/chat") public class ChatEndpoint { @OnMessage public void onMessage(Object message) { if (message instanceof String) { // handle text message } else if (message instanceof ByteBuffer) { // handle binary message } } }

In the above code, the onMessage method takes an Object parameter, which can be either a String or a ByteBuffer. The code checks the actual type of the incoming message using the instanceof operator and handles it accordingly.

Approach 4: Create separate WebSocket endpoints for each message type.

This approach involves creating separate WebSocket endpoints for handling text and binary messages. This approach is not recommended, as it adds unnecessary complexity to your application and can lead to performance issues.

In conclusion, Approach 1 and Approach 2 are the recommended ways to receive both text and binary messages in a WebSocket endpoint.