Saturday, 9 August 2014

Receive Message from client

The OnMessage annotation designates methods that handle incoming messages. You can have at most three methods annotated with @OnMessage in an endpoint, one for each message type: text, binary, and pong.

import javax.websocket.OnMessage;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.*;
import java.nio.ByteBuffer;

@ServerEndpoint("/receive")
public class ReceiveEndpoint {

    @OnMessage
   public void textMessage(Session session, String msg) {
      System.out.println("Text message: " + msg);
   }
   
   @OnMessage
   public void binaryMessage(Session session, ByteBuffer msg) {
      System.out.println("Binary message: " + msg.toString());
   }
   
   @OnMessage
   public void pongMessage(Session session, PongMessage msg) {
      System.out.println("Pong message: " + 
                          msg.getApplicationData().toString());
   }
}

 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment