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

26
LINES

< > BotCompany Repo | #1015083 // webSocketOnMessage - open a WebSocket and react to messages. Closed on program exit. Does not block

JavaX fragment (include) [tags: use-pretranspiled]

1  
!include once #1015699 // websocket
2  
3  
static WebSocketClient webSocketOnMessage(S url, final VF1<S> onMessage) {
4  
  ret webSocketOnMessage(url, onMessage, null);
5  
}
6  
7  
static WebSocketClient webSocketOnMessage(S url, final VF1<S> onMessage, fS initialMessage, fO... args) ctex {
8  
  WebSocketClient client = new WebSocketClient(new URI(url)) {
9  
    public void onMessage(S s) {
10  
      if (!licensed()) ret with close(); // program exit
11  
      // s = dropTrailingNullChars(s); // older server bug
12  
      pcallF(onMessage, s);
13  
    }
14  
15  
    public void onOpen(ServerHandshake handshake) {
16  
      if (nempty(initialMessage))
17  
        send(format(initialMessage, args));
18  
    }
19  
    
20  
    public void onClose(int code, S reason, bool remote) {}
21  
    public void onError(Exception e) { printStackTrace(e); }
22  
  };
23  
  
24  
  client.connect();
25  
  ret client;
26  
}

Author comment

Began life as a copy of #1015081

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1015083
Snippet name: webSocketOnMessage - open a WebSocket and react to messages. Closed on program exit. Does not block
Eternal ID of this version: #1015083/14
Text MD5: c526ef8085368529798be46f48633061
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-01 23:16:26
Source code size: 851 bytes / 26 lines
Pitched / IR pitched: No / No
Views / Downloads: 368 / 428
Version history: 13 change(s)
Referenced in: [show references]