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

36
LINES

< > BotCompany Repo | #1029959 // hInitWebSocket

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

Libraryless. Click here for Pure Java version (3079L/19K).

sS hInitWebSocket(O... _) {
  optPar S wsVar = "ws";
  optPar S onOpen;
  optPar S onMessage;
  optPar S readyMsg = "WebSocket ready!";
  ret hreconnectingWebSockets()
    + hscript([[
      var wsReady = false;
      var wsInitialMsgs = [];
      var wsVerbose = false;
      var ]] + wsVar + [[ = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/");
      
      ]] + wsVar + [[.onopen = function(event) {
        wsReady = true;
        console.log(]] + jsQuote(readyMsg) + [[);
        wsInitialMsgs.forEach(function(msg) {
          if (wsVerbose) console.log("Sending initial msg: " + msg);
          ws.send(msg);
        });
        ]] + jsDropTrailingComments(onOpen) + [[ };
        
      ]] + wsVar + [[.onmessage = function(event) {
        ]] + unnull(onMessage) + [[
      };
      
      // add an initial message to send when websocket opens
      // also send it now if ws is open already
      function wsInitialMsg(msg) {
        wsInitialMsgs.push(msg);
        if (wsReady) {
          if (wsVerbose) console.log("Sending initial msg: " + msg);
          ws.send(msg);
        }
      }
    ]]);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1029959
Snippet name: hInitWebSocket
Eternal ID of this version: #1029959/10
Text MD5: b8923a2ea6894e39b3c28d5399926627
Transpilation MD5: 70692c922304ad68bcb257d29dc3c73a
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-21 13:48:03
Source code size: 1221 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 250 / 400
Version history: 9 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1030367 - hInitSecondaryWebSocket
#1032264 - HInitWebSocket