Example usage for javax.websocket MessageHandler.Partial MessageHandler.Partial

List of usage examples for javax.websocket MessageHandler.Partial MessageHandler.Partial

Introduction

In this page you can find the example usage for javax.websocket MessageHandler.Partial MessageHandler.Partial.

Prototype

MessageHandler.Partial

Source Link

Usage

From source file:edu.rit.chrisbitler.ritcraft.slackintegration.rtm.RTMClient.java

/**
 * Method for when the websocket opens. We set up the message listener here too
 * @param session The websocket session that just opened for this method
 * @param endpointConfig The endpoint configuration - not used
 *//*from  ww w .  jav  a 2s . c o  m*/
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
    RTMClient.this.session = session;
    System.out.println("Websocket connection open");

    //Register the message handler - This has to be a MessageHandler.Partial. If it is a MessageHandler.Whole it won't work.
    session.addMessageHandler(new MessageHandler.Partial<String>() {
        @Override
        public void onMessage(String s, boolean b) {
            JSONObject obj = (JSONObject) JSONValue.parse(s);
            if (obj.get("type") != null) {
                String type = String.valueOf(obj.get("type"));

                //Give the message off to the handler based on the event type
                switch (type) {
                case "hello":
                    HelloMessage.handle();
                    break;
                case "message":
                    MsgMessage.handle(obj);
                    break;
                }
            } else {
                System.out.println("[Slack] Recieved malformed JSON message from slack - no type");
            }
        }
    });
}