It's impossible. JSR 356 clearly defines that there can only be one message handler in a text message: one per binary message and one per PongMessage . See @OnMessage javadoc:
======
This layer method annotation can be used to force the Java method to receive incoming web socket messages. Each endpoint website can have only one message processing method for each of the embedded websocket message formats: text, binary and pong. methods using this annotation is allowed to have the type parameters described below, otherwise the container will generate an error during deployment.
Allowed Options:
- Exactly one of the following options:
- if the method processes text messages:
- java.lang.String to receive the whole message
- Java primitive or class are equivalent to receive the whole message converted to this type
- String and logical pair to receive the message in parts
- java.io.Reader to get the whole message as a blocking thread.
- any object parameter for which the endpoint has a text decoder ( Decoder.Text or Decoder.TextStream ).
- if the method processes binary messages:
- If the method processes messages with pongs:
- and from zero to n String or primitive Java parameters annotated with javax.websocket.server.PathParam annotation for server endpoints.
- and optional Session parameter
Parameters can be listed in any order.
The method may have a non-empty return type, in which case the runtime web socket must interpret this as a web socket message in order 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 all for which there is an encoder. If the method uses the Java primitive as a return value, the implementation should build a text message to be sent using the standard Java representation of the Java primitive, unless the developer has provided an encoder for the type configured for this endpoint at which that encoder should be used. If the method uses the Java primitive class equivalent as the return value, the implementation should build a text message from the Java primitive equivalent, as described above.
Developers should note that if a developer closes a session while calling a method with a return type, method e will end, but the return value will not be delivered to the remote endpoint. A send error will be returned to the endpoint error handling method.
For instance:
@OnMessage public void processGreeting(String message, Session session) { System.out.println("Greeting received:" + message); }
For instance:
@OnMessage public void processUpload(byte[] b, boolean last, Session session) {
Developers should not continue to reference message objects such as
java.io.Reader ,
java.nio.ByteBuffer, or
java.io.InputStream after the annotated method completes, as they can be processed by the implementation.
source share