Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

113
LINES

< > BotCompany Repo | #1002611 // Test Slack RTM API

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Compilation Failed (1462L/11K).

!752

!1002612 // javax.websocket + implementation (tyrus)

import javax.websocket.*;

p {
  S token = devChannelToken();
  S url = "https://slack.com/api/rtm.start";
  Map postData = litmap("token", token, "no_unreads", "1");
  S data = doPost(postData, url);
  Map map = cast jsonDecode(data);
  //printStructure(map);
  
  S websocketURL = getString(map, "url");
  print("Websocket URL: " + websocketURL);
  assertTrue(nempty(websocketURL));
  
  // open websocket
  
  WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI(websocketURL));

  clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {
    public void handleMessage(String message) {
      print(">> " + message + " <<");
    }
  });

  // send message to websocket
  //clientEndPoint.sendMessage("{'event':'addChannel','channel':'ok_btccny_ticker'}");

  // wait 10 seconds for messages from websocket
  sleepSeconds(10);
}

/**
 * @author Jiji_Sasidharan
 */
static class WebsocketClientEndpoint {

    Session userSession = null;
    private MessageHandler messageHandler;

    public WebsocketClientEndpoint(URI endpointURI) {
        try {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            container.connectToServer(this, endpointURI);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Callback hook for Connection open events.
     *
     * @param userSession the userSession which is opened.
     */
    @OnOpen
    public void onOpen(Session userSession) {
        System.out.println("opening websocket");
        this.userSession = userSession;
    }

    /**
     * Callback hook for Connection close events.
     *
     * @param userSession the userSession which is getting closed.
     * @param reason the reason for connection close
     */
    @OnClose
    public void onClose(Session userSession, CloseReason reason) {
        System.out.println("closing websocket");
        this.userSession = null;
    }

    /**
     * Callback hook for Message Events. This method will be invoked when a client send a message.
     *
     * @param message The text message
     */
    @OnMessage
    public void onMessage(String message) {
        if (this.messageHandler != null) {
            this.messageHandler.handleMessage(message);
        }
    }

    /**
     * register message handler
     *
     * @param msgHandler
     */
    public void addMessageHandler(MessageHandler msgHandler) {
        this.messageHandler = msgHandler;
    }

    /**
     * Send a message.
     *
     * @param message
     */
    public void sendMessage(String message) {
        this.userSession.getAsyncRemote().sendText(message);
    }

    /**
     * Message handler.
     *
     * @author Jiji_Sasidharan
     */
    public static interface MessageHandler {
        public void handleMessage(String message);
    }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002611
Snippet name: Test Slack RTM API
Eternal ID of this version: #1002611/1
Text MD5: 02a07b88c7a7ef14fd38afe0aaf52657
Transpilation MD5: c0a6ab9d5be218a4e1962bd82ae237c1
Author: stefan
Category: javax / eleu
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-15 19:19:02
Source code size: 3043 bytes / 113 lines
Pitched / IR pitched: No / No
Views / Downloads: 692 / 680
Referenced in: [show references]