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

60
LINES

< > BotCompany Repo | #1014674 // WebSocketManager

JavaX fragment (include)

1  
static WebSocketManager webSocketManager;
2  
3  
sclass WebSocketManager {
4  
  Set webSockets = synchroHashSet();
5  
6  
  *() {
7  
    replaceCollection(webSockets, subBot_getMyWebSockets());
8  
    ifdef WebSocketManager_debug
9  
      print("Have " + n2(webSockets, "web socket") + ".");
10  
    endifdef
11  
  }
12  
13  
  void onNewWebSocket(O ws, S uri) {
14  
    ifdef WebSocketManager_debug
15  
      print("New web socket!");
16  
    endifdef
17  
    webSockets.add(ws);
18  
  }
19  
  
20  
  void onWebSocketMessage(O ws, S msg) {
21  
    ifdef WebSocketManager_debug
22  
      print("Got message: " + msg);
23  
    endifdef
24  
  }
25  
26  
  void onWebSocketClosed(O ws) {
27  
    ifdef WebSocketManager_debug
28  
      print("Closed web socket");
29  
    endifdef
30  
    webSockets.remove(ws);
31  
  }
32  
 
33  
  void sendToAllWebSockets(S msg) {
34  
    for (O ws : webSockets)
35  
      pcall(ws, 'send, msg);
36  
  }
37  
  
38  
  Collection webSockets() {
39  
    ret cloneList(webSockets);
40  
  }
41  
  
42  
  bool isEmpty() { ret empty(webSockets); }
43  
  
44  
  void close(O ws) { call(ws, 'close); }
45  
}
46  
47  
static void onNewWebSocket(O ws, S uri) {
48  
  if (webSocketManager != null)
49  
    webSocketManager.onNewWebSocket(ws, uri);
50  
}
51  
52  
static void onWebSocketMessage(O ws, S msg) {
53  
  if (webSocketManager != null)
54  
    webSocketManager.onWebSocketMessage(ws, msg);
55  
}
56  
57  
static void onWebSocketClosed(O ws) {
58  
  if (webSocketManager != null)
59  
    webSocketManager.onWebSocketClosed(ws);
60  
}

Author comment

Began life as a copy of #1014671

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1014674
Snippet name: WebSocketManager
Eternal ID of this version: #1014674/11
Text MD5: 6a8e91e52f1d073a1c80595b4ac19f3d
Author: stefan
Category: javax / websockets
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-23 19:47:14
Source code size: 1391 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 382 / 924
Version history: 10 change(s)
Referenced in: [show references]