-
@Retention(value=RUNTIME) @Target(value=METHOD) public @interface OnMessage
This method level annotation can be used to make a Java method receive incoming web socket messages. Each websocket endpoint may only have one message handling method for each of the native websocket message formats: text, binary and pong. Methods using this annotation are allowed to have parameters of types described below, otherwise the container will generate an error at deployment time.The allowed parameters are:
- Exactly one of any of the following choices
- if the method is handling text messages:
-
String
to receive the whole message - Java primitive or class equivalent to receive the whole message converted to that type
- String and boolean pair to receive the message in parts
-
Reader
to receive the whole message as a blocking stream - any object parameter for which the endpoint has a text decoder (
Decoder.Text
orDecoder.TextStream
).
-
- if the method is handling binary messages:
- byte[] or
ByteBuffer
to receive the whole message - byte[] and boolean pair, or
ByteBuffer
and boolean pair to receive the message in parts -
InputStream
to receive the whole message as a blocking stream - any object parameter for which the endpoint has a binary decoder (
Decoder.Binary
orDecoder.BinaryStream
).
- byte[] or
- if the method is handling pong messages:
-
PongMessage
for handling pong messages
-
- if the method is handling text messages:
- and Zero to n String or Java primitive parameters
annotated with the
PathParam
annotation for server endpoints. - and an optional
Session
parameter
The method may have a non-void return type, in which case the web socket runtime must interpret this as a web socket message to return to the peer. The allowed data types for this return type, other than void, are String, ByteBuffer, byte[], any Java primitive or class equivalent, and anything for which there is an encoder. If the method uses a Java primitive as a return value, the implementation must construct the text message to send using the standard Java string representation of the Java primitive unless there developer provided encoder for the type configured for this endpoint, in which case that encoder must be used. If the method uses a class equivalent of a Java primitive as a return value, the implementation must construct the text message from the Java primitive equivalent as described above.
Developers should note that if developer closes the session during the invocation of a method with a return type, the method will complete but the return value will not be delivered to the remote endpoint. The send failure will be passed back into the endpoint's error handling method.
For example:
For example:@OnMessage public void processGreeting(String message, Session session) { System.out.println("Greeting received:" + message); }
Developers should not continue to reference message objects of type@OnMessage public void processUpload(byte[] b, boolean last, Session session) { // process partial data here, which check on last to see if these is more on the way }
Reader
,ByteBuffer
orInputStream
after the annotated method has completed, since they may be recycled by the implementation.- Author:
- dannycoward
- Exactly one of any of the following choices
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element and Description long
maxMessageSize
Specifies the maximum size of message in bytes that the method this annotates will be able to process, or -1 to indicate that there is no maximum.
-
-
-
Element Detail
-
maxMessageSize
public abstract long maxMessageSize
Specifies the maximum size of message in bytes that the method this annotates will be able to process, or -1 to indicate that there is no maximum. The default is -1. This attribute only applies when the annotation is used to process whole messages, not to those methods that process messages in parts or use a stream or reader parameter to handle the incoming message. If the incoming whole message exceeds this limit, then the implementation generates an error and closes the connection using the reason that the message was too big.- Returns:
- the maximum size in bytes.
- Default:
- -1L
-
-
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/OnMessage.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
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.