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

1  
sS hInitWebSocket(O... _) {
2  
  optPar S wsVar = "ws";
3  
  optPar S onOpen;
4  
  optPar S onMessage;
5  
  optPar S readyMsg = "WebSocket ready!";
6  
  ret hreconnectingWebSockets()
7  
    + hscript([[
8  
      var wsReady = false;
9  
      var wsInitialMsgs = [];
10  
      var wsVerbose = false;
11  
      var ]] + wsVar + [[ = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/");
12  
      
13  
      ]] + wsVar + [[.onopen = function(event) {
14  
        wsReady = true;
15  
        console.log(]] + jsQuote(readyMsg) + [[);
16  
        wsInitialMsgs.forEach(function(msg) {
17  
          if (wsVerbose) console.log("Sending initial msg: " + msg);
18  
          ws.send(msg);
19  
        });
20  
        ]] + jsDropTrailingComments(onOpen) + [[ };
21  
        
22  
      ]] + wsVar + [[.onmessage = function(event) {
23  
        ]] + unnull(onMessage) + [[
24  
      };
25  
      
26  
      // add an initial message to send when websocket opens
27  
      // also send it now if ws is open already
28  
      function wsInitialMsg(msg) {
29  
        wsInitialMsgs.push(msg);
30  
        if (wsReady) {
31  
          if (wsVerbose) console.log("Sending initial msg: " + msg);
32  
          ws.send(msg);
33  
        }
34  
      }
35  
    ]]);
36  
}

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: 157 / 260
Version history: 9 change(s)
Referenced in: [show references]