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

64
LINES

< > BotCompany Repo | #1016453 // OS Instances Connector [Web Bot, Web Sockets]

JavaX module [tags: butter use-pretranspiled] - homepage

Download Jar. Libraryless. Click here for Pure Java version (3152L/20K/78K).

!7

set flag webSocketManager_debug.

static Map<O, State> wsToState = weakHashMap();
static long timeout = 30000+10000; // idle time + latency
static ConnectedInstances cI;
static ReliableSingleThread rst = new(r update);
static int theNumber;

sclass State {
  S computerID;
  int countSeen = -1;
}

p {
  cI = new ConnectedInstances;
  cI.connected2.onChange(rst);
  webSocketManager = new WebSocketManager {
    void onNewWebSocket(O ws, S uri) {
      super.onNewWebSocket(ws, uri);
      set(ws, 'timeout, timeout);
      S computerID = afterLastSlash(uri);
      if (!possibleComputerID(computerID)) computerID = "?";
      wsToState.put(ws, nu(State, +computerID));
      cI.gotConnection(computerID);
      rst.trigger();
    }
    
    void onWebSocketClosed(O ws) {
      super.onWebSocketClosed(ws);
      State s = wsToState.get(ws);
      if (s != null)
        cI.lostConnection(s.computerID);
      rst.trigger();
    }
    
    void onWebSocketMessage(O ws, S s) {}
  };
}

html { ret "Use WebSocket: " + subBot_myWebSocketURI(); }

svoid sendNumber(O ws) {
  State s = wsToState.get(ws);
  if (s != null && theNumber != s.countSeen) {
    s.countSeen = theNumber;
    print("Sending number to " + s.computerID + ": " + theNumber);
    call(ws, 'send, str(theNumber));
  }
}

svoid update {
  print("OS Instances update");
  if (webSocketManager.isEmpty()) ret;

  int value = cI.value();
  //if (value == theNumber) ret;
  theNumber = value;
  for (O ws : webSocketManager.webSockets()) pcall {
    sendNumber(ws);
  }
  print("OS Instances update done");
}

Author comment

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: 531 / 1708
Version history: 19 change(s)
Referenced in: [show references]