javax.websocket

Class Endpoint


  • public abstract class Endpoint
    extends Object
    The Web Socket Endpoint represents an object that can handle websocket conversations. Developers may extend this class in order to implement a programmatic websocket endpoint. The Endpoint class holds lifecycle methods that may be overridden to intercept websocket open, error and close events. 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. Each instance of a websocket endpoint is guaranteed not to be called by more than one thread at a time per active connection.

    If deployed as a client endpoint, it will be instantiated once for the single connection to the server.

    When deployed as a server endpoint, the implementation uses the ServerEndpointConfig.Configurator.getEndpointInstance(java.lang.Class<T>) method to obtain the endpoint instance it will use for each new client connection. If the developer uses the default ServerEndpointConfig.Configurator, there will be precisely one endpoint instance per active client connection. Consequently, in this typical case, when implementing/overriding the methods of Endpoint, the developer is guaranteed that there will be at most one thread calling each endpoint instance at a time.

    If the developer provides a custom ServerEndpointConfig.Configurator which overrides the default policy for endpoint instance creation, for example, using a single Endpoint instance for multiple client connections, the developer may need to write code that can execute concurrently.

    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
                     }
                 }
             });
         }
    
     }
     
    Author:
    dannycoward
    • Constructor Detail

      • Endpoint

        public Endpoint()
    • Method Detail

      • 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.

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 11/06/2005, zuletzt geändert 18/08/2025
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-javaee-rf-javax/websocket/endpoint.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:fr Manuel PHP : https://docs.oracle.com/en/java/, Class Endpoint

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor dieser Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.