Class Endpoint

java.lang.Object
javax.websocket.Endpoint

public abstract class Endpoint extends Object
The Web Socket Endpoint represents an object that can handle a web socket conversations. When deployed as a server endpoint, that is to say, the endpoint is registered to a URL, the server instantiates a new endpoint instance for each client connection. If deployed as a client, the endpoint will be instantiated once per single connection to the server. If the endpoint is a server which will cater to multiple clients, each endpoint instance corresponding to each active client is called by no more than one thread at a time. This means that when implementing/overriding the methods of Endpoint, the developer is guaranteed that there will be at most one thread in each endpoint instance.

Here is an example of a simple endpoint that echoes any incoming text message back to the sender.


 public class EchoServer extends Endpoint {

     public void onOpen(Session session, EndpointConfig config) {
         final RemoteEndpoint remote = session.getBasicRemote();
         session.addMessageHandler(new MessageHandler.Whole<String>() {
             public void onMessage(String text) {
                 try {
                     remote.sendString("Got your message (" + text + "). Thanks !");
                 } catch (IOException ioe) {
                     // handle send failure here
                 }
             }
         });
     }

 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    onClose(Session session, CloseReason closeReason)
    This method is called immediately prior to the session with the remote peer being closed.
    void
    onError(Session session, Throwable thr)
    Developers may implement this method when the web socket session creates some kind of error that is not modeled in the web socket protocol.
    abstract void
    onOpen(Session session, EndpointConfig config)
    Developers must implement this method to be notified when a new conversation has just begun.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Endpoint

      public Endpoint()
  • Method Details

    • onOpen

      public abstract void onOpen(Session session, EndpointConfig config)
      Developers must implement this method to be notified when a new conversation has just begun.
      Parameters:
      session - the session that has just been activated.
      config - the configuration used to configure this endpoint.
    • onClose

      public void onClose(Session session, CloseReason closeReason)
      This method is called immediately prior to the session with the remote peer being closed. It is called whether the session is being closed because the remote peer initiated a close and sent a close frame, or whether the local websocket container or this endpoint requests to close the session. The developer may take this last opportunity to retrieve session attributes such as the ID, or any application data it holds before it becomes unavailable after the completion of the method. Developers should not attempt to modify the session from within this method, or send new messages from this call as the underlying connection will not be able to send them at this stage.
      Parameters:
      session - the session about to be closed.
      closeReason - the reason the session was closed.
    • onError

      public void onError(Session session, Throwable thr)
      Developers may implement this method when the web socket session creates some kind of error that is not modeled in the web socket protocol. This may for example be a notification that an incoming message is too big to handle, or that the incoming message could not be encoded.

      There are a number of categories of exception that this method is (currently) defined to handle:

      • connection problems, for example, a socket failure that occurs before the web socket connection can be formally closed. These are modeled as SessionExceptions
      • runtime errors thrown by developer created message handlers calls.
      • conversion errors encoding incoming messages before any message handler has been called. These are modeled as DecodeExceptions
      Parameters:
      session - the session in use when the error occurs.
      thr - the throwable representing the problem.