Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // -----------------------------------------------------------------------
  3. //
  4. // LAYER 0: WEBRTC and SIGNALLING
  5. //
  6. // Provides low level webrtc and signalling functionality. This layer
  7. // exposes two API. The first is the Hub, which provides an interface
  8. // to smoke-hub signalling backend, the second is the Driver, which
  9. // provides port reservation, and listening interfaces for receiving
  10. // raw webrtc RTCDataChannels.
  11. //
  12. // -----------------------------------------------------------------------
  13.  
  14. // -----------------------------------------------------------------------
  15. // Hub: Provides access to this hubs binding to the virtual network
  16. // -----------------------------------------------------------------------
  17.  
  18. // Returns this hubs binding information
  19. const [address, configuration] = await node.hub.binding()
  20.  
  21. // Sends a message to a remote peer
  22. await node.hub.send(address, 'hello')
  23.  
  24. // Receives a message from a remote peer
  25. const [from, data] = await node.hub.receive()
  26.  
  27. // ------------------------------------------------------------------------
  28. // Driver: Binds and returns port reservations to receive peer connections
  29. // ------------------------------------------------------------------------
  30.  
  31. // Binds a port to receive data channels
  32. const listener = node.driver.bind(4000, { reliable: false })
  33.  
  34. // Enumerate incoming data channels. The peer may be shared per subsequent connections
  35. for await (const [peer, datachannel] of listener) {
  36.    
  37. }
  38. // Out of band: Dispose. Terminates listener iterator.
  39. listener.dispose()
  40.  
  41. // Enumerate incoming data channels. The peer may be shared per subsequent connections
  42. const [peer, datachannel] = await node.driver.connect('localhost', 4000)
  43.  
  44. // -----------------------------------------------------------------------
  45. //
  46. // LAYER 1: TCP, UDP
  47. //
  48. // This layer is responsible for emulating TCP and UDP protocols over
  49. // ordered | non-ordered RTCDataChannels respectively. This layer
  50. // directly interacts with LEVEL 0 API's.
  51. //
  52. // -----------------------------------------------------------------------
  53.  
  54. // -------------------------------------------------------------------
  55. // tcp
  56. // -------------------------------------------------------------------
  57.  
  58. const server = node.tcp.listen(3000)
  59.  
  60. for await(const socket of server) {
  61.  
  62.     socket.write('hello world')
  63.  
  64.     socket.end()
  65. }
  66.  
  67. //
  68. // server.dispose()
  69. //    |
  70. //    +--- driver: listener.dispose()
  71. //
  72. server.dispose()
  73.  
  74.  
  75. const socket = await node.tcp.connect('localhost', 5000)
  76. //  |
  77. //  + node.driver.connect('localhost', 5000)
  78.  
  79. for await(const buffer of socket) {
  80.    
  81.     // process
  82. }
  83.  
  84. // ---------------------------------------------------
  85. // LAYER: 2: APPLICATION PROTOCOLS
  86. //
  87. // HTTP and MEDIA application level functionality.
  88. // This level builds on top on the RTCDataChannel
  89. // exposes
  90. // ---------------------------------------------------
  91.  
  92. const server = node.http.listen(80)
  93.  
  94. for await(const [req, res] of server) {
  95.  
  96.     const buffers = []
  97.    
  98.     for await(const buffer of req) {
  99.  
  100.         buffers.push(buffer)
  101.     }
  102.  
  103.     await res.header(200, { })
  104.  
  105.     await res.write('hello')
  106.  
  107.     await res.end()
  108. }
  109.  
  110. // upgrade to web socket
  111.  
  112. for await(const [req, res] of server) {
  113.  
  114.     const socket = await req.upgradeToSocket()
  115.    
  116.     socket.send('1234')
  117.    
  118.     for await (const message of socket) {
  119.        
  120.         ///
  121.     }
  122. }
  123.  
  124. // ... later
  125.  
  126. server.dispose()
  127. // |
  128. // + --- net: server.dispose()
  129. //        |
  130. //        + ---- driver: listener.dispose()
  131.  
  132.  
  133. const data = await node.http.fetch('http://localhost:80').then(res => res.text())
  134.  
  135. // ---------------------------------------------------
  136. // mediastream
  137. // ---------------------------------------------------
  138.  
  139. const mediastream = node.media.pattern()
  140.  
  141. for await(const client of node.media.listen(1000)) {
  142.  
  143.     await client.send(mediastream)
  144.  
  145.     await client.end()
  146. }
  147.  
  148. const client = node.media.connect('localhost', 5000)
  149.  
  150. const mediastream = await client.receive()
  151.  
  152. await client.end()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement