Java – WebSockets with multiple data in onmessage annotation

I'm using WebSockets I want to use multiple @ onmessage overloaded methods with different data types

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

@OnMessage
public void onMessage(String message) {
    System.out.println(message);
}

Message is a POJO class, which is decoded and encoded

On the 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 received 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)

Anyone can advise us how to send data to or from the server using multiple types of data

Solution

It's impossible. JSR 356 clearly defines that each text message can only have one message handler, one for each binary message and one for each pongmessage See @ onmessage Javadoc:

======

This method level annotation can be used to make Java methods receive incoming web socket messages Each websocket endpoint may only have one message processing method for each native websocket message format: text, binary, and pong Method allows parameters of the type described below in this comment, otherwise the container will generate an error at deployment time The allowed parameters are: > any of the following options > if the method is processing SMS: > java Lang.string receiving the whole message > java primitive or class is equivalent to receiving the whole message converted to this type > string and Boolean pairs to partially receive the message > java io. The reader receives the entire message as a blocking stream > any object parameter (decoder.text or decoder. TextStream) with a text decoder at the endpoint. > If the method is processing binary messages: > byte [] or Java nio. Byte buffer receives the entire message > byte [] and Boolean pairs, or Java nio. ByteBuffer and Boolean pairs are used to receive some messages > java io. InputStream receives the entire message as a blocking stream > any object parameter (decoder.binary or decoder. Binarystream) with a binary decoder at the endpoint. > If the method is processing a Pong message: > pongmessage is used to process ping pong messages > and zero to n strings or Java raw parameters, use the javax of the server endpoint websocket. server. Pathparam comment. > And an optional session parameter, which can be listed in any order

This method can have a non void return type, in which case it is a web socket. The runtime must interpret this as a web socket message to return to the peer The allowed data types of this return type (except void) are string, ByteBuffer, byte [], any Java primitive or class equivalent, and which has an encoder If the method uses a Java primitive as the return value, the implementation must construct a standard Java string representation of the Java primitive used to send text messages, unless a developer provides an encoder of the type configured for this endpoint, where the encoder must be used If the method uses a class equivalent Java primitive as the return value, the text message must be constructed 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, the e method will complete, but the return value will not be passed to the remote endpoint The error handling method to pass the sending failure back to the endpoint

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 refer to Java io. Reader,java. nio. The message object or annotation method of ByteBuffer is completed io. Inputstreams because they can be recycled through implementation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>