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).

1  
!752
2  
!1002612 // javax.websocket + implementation (tyrus)
3  
4  
import javax.websocket.*;
5  
6  
p {
7  
  S token = devChannelToken();
8  
  S url = "https://slack.com/api/rtm.start";
9  
  Map postData = litmap("token", token, "no_unreads", "1");
10  
  S data = doPost(postData, url);
11  
  Map map = cast jsonDecode(data);
12  
  //printStructure(map);
13  
  
14  
  S websocketURL = getString(map, "url");
15  
  print("Websocket URL: " + websocketURL);
16  
  assertTrue(nempty(websocketURL));
17  
  
18  
  // open websocket
19  
  
20  
  WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI(websocketURL));
21  
22  
  clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {
23  
    public void handleMessage(String message) {
24  
      print(">> " + message + " <<");
25  
    }
26  
  });
27  
28  
  // send message to websocket
29  
  //clientEndPoint.sendMessage("{'event':'addChannel','channel':'ok_btccny_ticker'}");
30  
31  
  // wait 10 seconds for messages from websocket
32  
  sleepSeconds(10);
33  
}
34  
35  
/**
36  
 * @author Jiji_Sasidharan
37  
 */
38  
static class WebsocketClientEndpoint {
39  
40  
    Session userSession = null;
41  
    private MessageHandler messageHandler;
42  
43  
    public WebsocketClientEndpoint(URI endpointURI) {
44  
        try {
45  
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
46  
            container.connectToServer(this, endpointURI);
47  
        } catch (Exception e) {
48  
            throw new RuntimeException(e);
49  
        }
50  
    }
51  
52  
    /**
53  
     * Callback hook for Connection open events.
54  
     *
55  
     * @param userSession the userSession which is opened.
56  
     */
57  
    @OnOpen
58  
    public void onOpen(Session userSession) {
59  
        System.out.println("opening websocket");
60  
        this.userSession = userSession;
61  
    }
62  
63  
    /**
64  
     * Callback hook for Connection close events.
65  
     *
66  
     * @param userSession the userSession which is getting closed.
67  
     * @param reason the reason for connection close
68  
     */
69  
    @OnClose
70  
    public void onClose(Session userSession, CloseReason reason) {
71  
        System.out.println("closing websocket");
72  
        this.userSession = null;
73  
    }
74  
75  
    /**
76  
     * Callback hook for Message Events. This method will be invoked when a client send a message.
77  
     *
78  
     * @param message The text message
79  
     */
80  
    @OnMessage
81  
    public void onMessage(String message) {
82  
        if (this.messageHandler != null) {
83  
            this.messageHandler.handleMessage(message);
84  
        }
85  
    }
86  
87  
    /**
88  
     * register message handler
89  
     *
90  
     * @param msgHandler
91  
     */
92  
    public void addMessageHandler(MessageHandler msgHandler) {
93  
        this.messageHandler = msgHandler;
94  
    }
95  
96  
    /**
97  
     * Send a message.
98  
     *
99  
     * @param message
100  
     */
101  
    public void sendMessage(String message) {
102  
        this.userSession.getAsyncRemote().sendText(message);
103  
    }
104  
105  
    /**
106  
     * Message handler.
107  
     *
108  
     * @author Jiji_Sasidharan
109  
     */
110  
    public static interface MessageHandler {
111  
        public void handleMessage(String message);
112  
    }
113  
}

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: 627 / 596
Referenced in: [show references]