Saturday, 9 August 2014

Programmatic server endpoint

Programmatic endpoints are created by extending 'javax.websocket.Endpoint' class. Then Endpoint class defines three life cycle method, in which two concrete(onClose, onError) and one abstract (onOpen) method. So the classes which extends this class must override the 'onOpen' method.

Methods in Endpoint class
Method Description
public abstract void onOpen(Session session, EndpointConfig config) Developers must implement this method to be notified when a new conversation has just begun.
public void onClose(Session session, CloseReason closeReason) Called immediately prior to the session with the remote peer being closed.
public void onError(Session session, Throwable thr) Can override this method, to handle error conditions.

By implementing the onOpen method, the programmatic endpoint gains access to the Session object, to which the developer may add MessageHandler implementations in order to intercept incoming websocket messages.

Creating a Programmatic end point is a 4 step process.

Step 1: Create an endpoint class by extending Endpoint class.
Step 2: Implement the lifecycle methods of the endpoint.
Ex: onOpen(Session session, EndpointConfig config)
Step 3: Add your business logic to the endpoint.
Step 4: Deploy the endpoint inside a web application.
ServerEndpointConfig.Builder.create(EchoEndpoint.class, "/echo").build();


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment