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

134
LINES

< > BotCompany Repo | #1029697 // "Scribble" [LIVE at scribble.gazelle.rocks]

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

Uses 1113K of libraries. Click here for Pure Java version (4138L/22K).

1  
!7
2  
3  
cm 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  
12  
    if (eq(uri, "/script")) 
13  
      ret subBot_serveJavaScript(linesLL(
14  
        "document.body.innerHTML += " + jsQuote(mainElements()) + ";",
15  
        "document.body.innerHTML += " + jsQuote(mainCSS()) + ";",
16  
        loadSnippet(#1013374), // ReconnectingWebSocket
17  
        loadSnippet(#1029696), // scribble.js
18  
        mainJSCode()
19  
      ));
20  
21  
    if (eq(uri, "/scriptTest"))
22  
      ret hhtml(hhead(
23  
        htitle("Scribble Embed Test")
24  
        + loadJQuery())
25  
        + hbody(hjavascript_src("https://www.actualbackground.website/script")));
26  
27  
    ret hhtml(hhead(htitle("Scribble")
28  
      + loadJQuery()
29  
      + hreconnectingWebSockets()
30  
      + mainCSS())
31  
      + hbody(
32  
        hfulltag canvas("", id := "scribble")
33  
        + div("?", id := "scribblePeopleCounter")
34  
        + hjssnippet(#1029696)
35  
        + hjs(mainJSCode())
36  
      ));
37  
  }
38  
39  
  S mainElements() {
40  
    ret hfulltag canvas("", id := "scribble")
41  
      + div("?", id := "scribblePeopleCounter");
42  
  }
43  
44  
  S mainCSS() {
45  
    ret hcss([[
46  
      /*body { margin: 0 !important; padding: 0 !important; }*/
47  
      
48  
      #scribble {
49  
        position: absolute;
50  
        top: 0; left: 0;
51  
        z-index: 100;
52  
      }
53  
      
54  
      #scribblePeopleCounter {
55  
        position: absolute;
56  
        top: 10px; right: 10px;
57  
        width: 100px;
58  
        text-align: right;
59  
        z-index: 101;
60  
        pointer-events: none;
61  
        color: white;
62  
      }
63  
    ]]);
64  
  }
65  
66  
  S mainJSCode() {
67  
    ret [[
68  
      function resized() {
69  
        canvas.width = document.body.clientWidth;
70  
        canvas.height = document.body.clientHeight;
71  
      }
72  
      resized();
73  
      $(window).bind("resize", resized);
74  
    
75  
      // JavaScript WebSocket handling
76  
      var wsReady = false;
77  
      var ws = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/");
78  
      //var ws = new ReconnectingWebSocket("wss://www.actualbackground.website");
79  
      ws.onopen = function(event) { wsReady = true; };
80  
      ws.onmessage = function(event) {
81  
        var data = JSON.parse(event.data);
82  
        if (data.counter)
83  
          $("#scribblePeopleCounter").html(data.counter);
84  
        else
85  
          drawStroke(data.x1, data.y1, data.x2, data.y2, data.mouseDown);
86  
      };
87  
      sendStroke = function(data) {
88  
        //console.log(data);
89  
        if (wsReady)
90  
          ws.send(JSON.stringify(data));
91  
      };
92  
    ]];
93  
  }
94  
  
95  
  void handleWebSocket(virtual WebSocket ws) {
96  
    set(ws, onClose := r { webSockets.remove(ws); wsCounterUpdate(); });
97  
    
98  
    setFieldToIVF1Proxy(ws, onMessage := msg -> { temp enter(); pcall {
99  
      S data = rcall_string getTextPayload(msg);
100  
      distributeEvent(data, ws);
101  
    }});
102  
103  
    set(ws, onOpen := rEnter {  
104  
      print("WebSocket opened!");
105  
      webSockets.add(ws); wsCounterUpdate();
106  
    });
107  
  }
108  
109  
  void wsCounterUpdate {
110  
    distributeEvent(jsonEncode(litmap(counter := nUsers(webSockets) + " online")));
111  
  }
112  
113  
  void distributeEvent(S data, virtual WebSocket sendingWebSocket default null) {
114  
    L<virtual WebSocket> sockets = cloneList(webSockets);
115  
    print("Distributing event to " + n2(sockets, "web socket") + ": " + data);
116  
    for (virtual WebSocket ws : sockets) pcall {
117  
      if (ws == sendingWebSocket) continue;
118  
      try {
119  
        call(ws, "send", data);
120  
      } catch e {
121  
        print("Removing faulty WebSocket");
122  
        webSockets.remove(ws); wsCounterUpdate();
123  
        call(ws, "close");
124  
      }
125  
    }
126  
  }
127  
128  
  void cleanMeUp_webSockets {
129  
    for (virtual WebSocket ws : cloneAndClearList(webSockets)) {
130  
      print("Closing WebSocket");
131  
      close((AutoCloseable) ws);
132  
    }
133  
  }
134  
}

Author comment

Began life as a copy of #1010027

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: #1029697
Snippet name: "Scribble" [LIVE at scribble.gazelle.rocks]
Eternal ID of this version: #1029697/42
Text MD5: fa6a0e3465e06a61be3e96df081c0439
Transpilation MD5: 8d2c31739436c98af186637b05d85700
Author: stefan
Category: javax / networking
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-24 21:57:30
Source code size: 3996 bytes / 134 lines
Pitched / IR pitched: No / No
Views / Downloads: 189 / 31985
Version history: 41 change(s)
Referenced in: [show references]