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]

!include once #1015699 // websocket

static WebSocketClient webSocketOnMessage(S url, final VF1<S> onMessage) {
  ret webSocketOnMessage(url, onMessage, null);
}

static WebSocketClient webSocketOnMessage(S url, final VF1<S> onMessage, fS initialMessage, fO... args) ctex {
  WebSocketClient client = new WebSocketClient(new URI(url)) {
    public void onMessage(S s) {
      if (!licensed()) ret with close(); // program exit
      // s = dropTrailingNullChars(s); // older server bug
      pcallF(onMessage, s);
    }

    public void onOpen(ServerHandshake handshake) {
      if (nempty(initialMessage))
        send(format(initialMessage, args));
    }
    
    public void onClose(int code, S reason, bool remote) {}
    public void onError(Exception e) { printStackTrace(e); }
  };
  
  client.connect();
  ret client;
}

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: 298 / 355
Version history: 13 change(s)
Referenced in: [show references]