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

1  
!7
2  
3  
cmodule ScribbleDemo > DynPrintLog {
4  
  !include #1029545 // API for Eleu
5  
6  
  transient Set<virtual WebSocket> webSockets = syncWeakHashSet();
7  
8  
  O html(virtual Request request) {
9  
    S uri = cast get(request, 'uri);
10  
    SS params = cast get(request, 'params);
11  
    ret hhtml(hhead(htitle("Scribble")
12  
      + loadJQuery()
13  
      + hreconnectingWebSockets()
14  
      + hcss([[
15  
        body { margin: 0 !important; padding: 0 !important; }
16  
        #peopleCounter {
17  
          position: absolute;
18  
          top: 10px; right: 10px;
19  
          width: 100px;
20  
          text-align: right;
21  
          z-index: 1000;
22  
          color: white;
23  
        }
24  
      ]]))
25  
      + hbody(
26  
        hfulltag canvas("", id := "scribble")
27  
        + div("?", id := "peopleCounter")
28  
        + hjssnippet(#1029696)
29  
        + hjs([[
30  
          function resized() {
31  
            canvas.width = document.body.clientWidth;
32  
            canvas.height = document.body.clientHeight;
33  
          }
34  
          resized();
35  
          $(window).bind("resize", resized);
36  
37  
          // JavaScript WebSocket handling
38  
          var wsReady = false;
39  
          var ws = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/");
40  
          ws.onopen = function(event) { wsReady = true; };
41  
          ws.onmessage = function(event) {
42  
            var data = JSON.parse(event.data);
43  
            if (data.counter)
44  
              $("#peopleCounter").html(data.counter);
45  
            else
46  
              drawStroke(data.x1, data.y1, data.x2, data.y2, data.mouseDown);
47  
          };
48  
          sendStroke = function(data) {
49  
            //console.log(data);
50  
            if (wsReady)
51  
              ws.send(JSON.stringify(data));
52  
          };
53  
        ]])
54  
      ));
55  
  }
56  
  
57  
  void handleWebSocket(virtual WebSocket ws) {
58  
    set(ws, onClose := r { webSockets.remove(ws); wsCounterUpdate(); });
59  
    
60  
    setFieldToIVF1Proxy(ws, onMessage := msg -> { temp enter(); pcall {
61  
      S data = rcall_string getTextPayload(msg);
62  
      distributeEvent(data, ws);
63  
    }});
64  
65  
    set(ws, onOpen := rEnter {  
66  
      print("WebSocket opened!");
67  
      webSockets.add(ws); wsCounterUpdate();
68  
    });
69  
  }
70  
71  
  void wsCounterUpdate {
72  
    distributeEvent(jsonEncode(litmap(counter := nUsers(webSockets) + " online")));
73  
  }
74  
75  
  void distributeEvent(S data, virtual WebSocket sendingWebSocket default null) {
76  
    L<virtual WebSocket> sockets = cloneList(webSockets);
77  
    print("Distributing event to " + n2(sockets, "web socket") + ": " + data);
78  
    for (virtual WebSocket ws : sockets) pcall {
79  
      if (ws == sendingWebSocket) continue;
80  
      try {
81  
        call(ws, "send", data);
82  
      } catch e {
83  
        print("Removing faulty WebSocket");
84  
        webSockets.remove(ws); wsCounterUpdate();
85  
        call(ws, "close");
86  
      }
87  
    }
88  
  }
89  
}

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: 161 / 220
Referenced in: [show references]