Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websockets with multiple datas in onMessage annotation

Tags:

java

websocket

I am using websockets. I want to use multiple @onMessage overloaded methods with different data types. In 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 pojo class and decoded and encoded it.

In 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 am getting 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 any one suggest us how to use multiple types of data sending to/from the server.

like image 489
user2350138 Avatar asked Feb 27 '14 07:02

user2350138


People also ask

What is Onmessage WebSocket?

The WebSocket. onmessage property is an EventHandler that is called when a message is received from the server. It is called with a MessageEvent .

Can Websockets drop messages?

Websocket client connections may drop due to intermittent network issue and when connections drop, messages will also be lost.

What is WebSocket container?

A WebSocketContainer is an implementation provided object that provides applications a view on the container running it. The WebSocketContainer container various configuration parameters that control default session and buffer properties of the endpoints it contains.


1 Answers

This is not possible. JSR 356 defines clearly that there can be only one message handler per text message, one per binary message and one per PongMessage. See @OnMessage javadoc:

======

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:

  1. Exactly one of any of the following choices
    • if the method is handling text messages:
      • java.lang.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
      • java.io.Reader to receive the whole message as a blocking stream
      • any object parameter for which the endpoint has a text decoder (Decoder.Text or Decoder.TextStream).
    • if the method is handling binary messages:
      • byte[] or java.nio.ByteBuffer to receive the whole message
      • byte[] and boolean pair, or java.nio.ByteBuffer and boolean pair to receive the message in parts
      • java.io.InputStream to receive the whole message as a blocking stream
      • any object parameter for which the endpoint has a binary decoder (Decoder.Binary or Decoder.BinaryStream).
    • if the method is handling pong messages:
      • PongMessage for handling pong messages
  2. and Zero to n String or Java primitive parameters annotated with the javax.websocket.server.PathParam annotation for server endpoints.
  3. and an optional Session parameter
The parameters may be listed in any order.

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, e 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:


 @OnMessage
 public void processGreeting(String message, Session session) {
     System.out.println("Greeting received:" + message);
 }
 
For example:

 @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 of type java.io.Reader, java.nio.ByteBuffer or java.io.InputStream after the annotated method has completed, since they may be recycled by the implementation.
like image 180
Pavel Bucek Avatar answered Oct 21 '22 05:10

Pavel Bucek