summaryrefslogtreecommitdiff
path: root/app/lib/websockets.hoon
blob: a87f2a104fb1816cb4945ced9fa759f8b9da0e0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|%
  ++  connect  |=  [endpoint=@t =bowl:gall]
    ^-  card:agent:gall
    =/  =task:iris  [%websocket-connect dap.bowl endpoint]
    [%pass /ws-connect %arvo %i task]

  ++  cancel-connect  |=  wid=@ud
    ^-  card:agent:gall
    =/  =task:iris  [%cancel-websocket wid]
    [%pass /ws-connect %arvo %i task]

  ++  disconnect  |=  wid=@ud
    ^-  card:agent:gall
    =/  =path  /websocket-client/(scot %ud wid)
    =/  ws-paths  :~(path)
    [%give %fact ws-paths %disconnect !>(~)]
  ::
  ++  give-ws-payload-client
    |=  [wid=@ msg=websocket-message:eyre]
    ^-  card:agent:gall
    =/  =cage
      [%message !>(msg)]
    =/  wsid  (scot %ud wid)
    [%give %fact ~[/websocket-client/[wsid]] cage]
  ++  close-ws-client
    |=  wid=@
    ^-  card:agent:gall
    =/  =cage
      [%disconnect !>(~)]
    =/  wsid  (scot %ud wid)
    [%give %fact ~[/websocket-client/[wsid]] cage]

  ++  give-ws-payload-server
    |=  [wid=@ event=websocket-event:eyre]
    ^-  card:agent:gall
    =/  =cage
      [%websocket-response !>([wid event])]
    =/  wsid  (scot %ud wid)
    [%give %fact ~[/websocket-server/[wsid]] cage]
  
  ++  accept-handshake  |=  wid=@
    =/  response  [%accept ~]
    :~
    (give-ws-payload-server wid response)
    ==
  ++  refuse-handshake  |=  wid=@
    =/  response  [%reject ~]
    :~
    (give-ws-payload-server wid response)
    ==
::  %iris scries
  +$  socket  [wid=@ud url=@t status=$?(%accepted %pending)]
  ++  get-url
    |=  [wid=@ud =bowl:gall]  ^-  (unit socket)
    =/  scry-path=path  /(scot %p our.bowl)/ws/(scot %da now.bowl)/id/(scot %ud wid)
    .^((unit socket) %ix scry-path)
  ++  check-connected
    |=  [url=@t =bowl:gall]  ^-  (unit socket)
    =/  scry-path=path  /(scot %p our.bowl)/ws/(scot %da now.bowl)/url/[url]
    .^((unit socket) %ix scry-path)
  ++  list-connected
    |=  =bowl:gall  ^-  (list socket)
    =/  scry-path=path  /(scot %p our.bowl)/ws/(scot %da now.bowl)/app
    .^((list socket) %ix scry-path)
--