Download Jar. Libraryless. Click here for Pure Java version (3152L/20K/78K).
1 | !7 |
2 | |
3 | set flag webSocketManager_debug. |
4 | |
5 | static Map<O, State> wsToState = weakHashMap(); |
6 | static long timeout = 30000+10000; // idle time + latency |
7 | static ConnectedInstances cI; |
8 | static ReliableSingleThread rst = new(r update); |
9 | static int theNumber; |
10 | |
11 | sclass State { |
12 | S computerID; |
13 | int countSeen = -1; |
14 | } |
15 | |
16 | p { |
17 | cI = new ConnectedInstances; |
18 | cI.connected2.onChange(rst); |
19 | webSocketManager = new WebSocketManager { |
20 | void onNewWebSocket(O ws, S uri) { |
21 | super.onNewWebSocket(ws, uri); |
22 | set(ws, 'timeout, timeout); |
23 | S computerID = afterLastSlash(uri); |
24 | if (!possibleComputerID(computerID)) computerID = "?"; |
25 | wsToState.put(ws, nu(State, +computerID)); |
26 | cI.gotConnection(computerID); |
27 | rst.trigger(); |
28 | } |
29 | |
30 | void onWebSocketClosed(O ws) { |
31 | super.onWebSocketClosed(ws); |
32 | State s = wsToState.get(ws); |
33 | if (s != null) |
34 | cI.lostConnection(s.computerID); |
35 | rst.trigger(); |
36 | } |
37 | |
38 | void onWebSocketMessage(O ws, S s) {} |
39 | }; |
40 | } |
41 | |
42 | html { ret "Use WebSocket: " + subBot_myWebSocketURI(); } |
43 | |
44 | svoid sendNumber(O ws) { |
45 | State s = wsToState.get(ws); |
46 | if (s != null && theNumber != s.countSeen) { |
47 | s.countSeen = theNumber; |
48 | print("Sending number to " + s.computerID + ": " + theNumber); |
49 | call(ws, 'send, str(theNumber)); |
50 | } |
51 | } |
52 | |
53 | svoid update { |
54 | print("OS Instances update"); |
55 | if (webSocketManager.isEmpty()) ret; |
56 | |
57 | int value = cI.value(); |
58 | //if (value == theNumber) ret; |
59 | theNumber = value; |
60 | for (O ws : webSocketManager.webSockets()) pcall { |
61 | sendNumber(ws); |
62 | } |
63 | print("OS Instances update done"); |
64 | } |
Began life as a copy of #1015684
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1016453 |
Snippet name: | OS Instances Connector [Web Bot, Web Sockets] |
Eternal ID of this version: | #1016453/20 |
Text MD5: | 55f5ed97229ea2f1430ec57fa111bb6e |
Transpilation MD5: | c0cc39fc8248e0de5c5f141073d189b1 |
Author: | stefan |
Category: | javax / a.i. / web |
Type: | JavaX module |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-06-24 00:50:05 |
Source code size: | 1638 bytes / 64 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 532 / 1714 |
Version history: | 19 change(s) |
Referenced in: | [show references] |