Multiple data Web sites in onMessage annotation

I am using websockets. I want to use several overloaded @onMessage methods with different data types. On the client side, I a have the following methods

@OnMessage public void onMessage(Message message) { System.out.println(message.getContent()+":"+message.getSubject()); } @OnMessage public void onMessage(String message) { System.out.println(message); } 

Where Message is a pojo class is decoded and encoded.

Server side

  @OnMessage public void onMessage(String msg, Session session) { try { System.out.println("Receive Message:" + msg); session.getBasicRemote().sendText("{\"subject\":\"This is subject1\",\"content\":\"This is content1\"}"); session.getBasicRemote().sendText("This is Example Test"); } catch (IOException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } } 

I get the following error

 javax.websocket.DeploymentException: Class: clientwebsocket.MyClient. Text MessageHandler already registered. at org.glassfish.tyrus.core.ErrorCollector.composeComprehensiveException(ErrorCollector.java:83) at org.glassfish.tyrus.client.ClientManager$1.run(ClientManager.java:384) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:565) at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:110) at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:343) at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:182) at clientwebsocket.ClientWebSocket.start(ClientWebSocket.java:31) at clientwebsocket.ClientWebSocket.main(ClientWebSocket.java:40) 

can anyone suggest us how to use several types of sending data to / from server.

+6
source share
1 answer

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) { // process partial data here, which check on last to see if these is more on the way } 
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.
+11
source

All Articles