Message handlers

Message handlers are used to manage messages according to the type.

Developers must implement message handlers so that they can receive incoming messages through a WebSocket conversation. Each WebSocket session works on one thread at a time to call its own message handlers. So, because each message handler instance must be used to handle messages for only one WebSocket session, at most one thread at a time can be calling its methods. Programmers who wish to work with messages from multiple clients inside the same message handlers can add the same instance as a handler on each WebSocket session of the clients. In that case, they must write code while paying attention that their message handler will be called concurrently by multiple threads, each one coming from a different session client side.

WebSocket specifications let you create a custom message handler. Here is a sample:

public class SessionMessageHandler implements Whole<PongMessage> {
private Logger logger = getLogger(SessionMessageHandler.class.getName());
@Override
public void onMessage(PongMessage message) {
logger.info("message: " + message);
}
}

This handler is used, for example, to decorate pong messages logging the message. A message handler can be of two types, Whole and Partial. These types extend the javax.websocket.MessageHandler interface, which provides the onMessage method. See the details in the next paragraphs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.29.145