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

89
LINES

< > BotCompany Repo | #1029699 // "Scribble" backup [WORKS]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (2633L/15K).

!7

cmodule ScribbleDemo > DynPrintLog {
  !include #1029545 // API for Eleu

  transient Set<virtual WebSocket> webSockets = syncWeakHashSet();

  O html(virtual Request request) {
    S uri = cast get(request, 'uri);
    SS params = cast get(request, 'params);
    ret hhtml(hhead(htitle("Scribble")
      + loadJQuery()
      + hreconnectingWebSockets()
      + hcss([[
        body { margin: 0 !important; padding: 0 !important; }
        #peopleCounter {
          position: absolute;
          top: 10px; right: 10px;
          width: 100px;
          text-align: right;
          z-index: 1000;
          color: white;
        }
      ]]))
      + hbody(
        hfulltag canvas("", id := "scribble")
        + div("?", id := "peopleCounter")
        + hjssnippet(#1029696)
        + hjs([[
          function resized() {
            canvas.width = document.body.clientWidth;
            canvas.height = document.body.clientHeight;
          }
          resized();
          $(window).bind("resize", resized);

          // JavaScript WebSocket handling
          var wsReady = false;
          var ws = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/");
          ws.onopen = function(event) { wsReady = true; };
          ws.onmessage = function(event) {
            var data = JSON.parse(event.data);
            if (data.counter)
              $("#peopleCounter").html(data.counter);
            else
              drawStroke(data.x1, data.y1, data.x2, data.y2, data.mouseDown);
          };
          sendStroke = function(data) {
            //console.log(data);
            if (wsReady)
              ws.send(JSON.stringify(data));
          };
        ]])
      ));
  }
  
  void handleWebSocket(virtual WebSocket ws) {
    set(ws, onClose := r { webSockets.remove(ws); wsCounterUpdate(); });
    
    setFieldToIVF1Proxy(ws, onMessage := msg -> { temp enter(); pcall {
      S data = rcall_string getTextPayload(msg);
      distributeEvent(data, ws);
    }});

    set(ws, onOpen := rEnter {  
      print("WebSocket opened!");
      webSockets.add(ws); wsCounterUpdate();
    });
  }

  void wsCounterUpdate {
    distributeEvent(jsonEncode(litmap(counter := nUsers(webSockets) + " online")));
  }

  void distributeEvent(S data, virtual WebSocket sendingWebSocket default null) {
    L<virtual WebSocket> sockets = cloneList(webSockets);
    print("Distributing event to " + n2(sockets, "web socket") + ": " + data);
    for (virtual WebSocket ws : sockets) pcall {
      if (ws == sendingWebSocket) continue;
      try {
        call(ws, "send", data);
      } catch e {
        print("Removing faulty WebSocket");
        webSockets.remove(ws); wsCounterUpdate();
        call(ws, "close");
      }
    }
  }
}

Author comment

Began life as a copy of #1029697

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1029699
Snippet name: "Scribble" backup [WORKS]
Eternal ID of this version: #1029699/1
Text MD5: 6c756e928e7c40b34ed0ee828e78211c
Transpilation MD5: ce2ecee1fabfa27ae108e577944ce8c1
Author: stefan
Category: javax / networking
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-07 19:51:14
Source code size: 2887 bytes / 89 lines
Pitched / IR pitched: No / No
Views / Downloads: 217 / 292
Referenced in: