Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -----------------------------------------------------------------------
- //
- // LAYER 0: WEBRTC and SIGNALLING
- //
- // Provides low level webrtc and signalling functionality. This layer
- // exposes two API. The first is the Hub, which provides an interface
- // to smoke-hub signalling backend, the second is the Driver, which
- // provides port reservation, and listening interfaces for receiving
- // raw webrtc RTCDataChannels.
- //
- // -----------------------------------------------------------------------
- // -----------------------------------------------------------------------
- // Hub: Provides access to this hubs binding to the virtual network
- // -----------------------------------------------------------------------
- // Returns this hubs binding information
- const [address, configuration] = await node.hub.binding()
- // Sends a message to a remote peer
- await node.hub.send(address, 'hello')
- // Receives a message from a remote peer
- const [from, data] = await node.hub.receive()
- // ------------------------------------------------------------------------
- // Driver: Binds and returns port reservations to receive peer connections
- // ------------------------------------------------------------------------
- // Binds a port to receive data channels
- const listener = node.driver.bind(4000, { reliable: false })
- // Enumerate incoming data channels. The peer may be shared per subsequent connections
- for await (const [peer, datachannel] of listener) {
- }
- // Out of band: Dispose. Terminates listener iterator.
- listener.dispose()
- // Enumerate incoming data channels. The peer may be shared per subsequent connections
- const [peer, datachannel] = await node.driver.connect('localhost', 4000)
- // -----------------------------------------------------------------------
- //
- // LAYER 1: TCP, UDP
- //
- // This layer is responsible for emulating TCP and UDP protocols over
- // ordered | non-ordered RTCDataChannels respectively. This layer
- // directly interacts with LEVEL 0 API's.
- //
- // -----------------------------------------------------------------------
- // -------------------------------------------------------------------
- // tcp
- // -------------------------------------------------------------------
- const server = node.tcp.listen(3000)
- for await(const socket of server) {
- socket.write('hello world')
- socket.end()
- }
- //
- // server.dispose()
- // |
- // +--- driver: listener.dispose()
- //
- server.dispose()
- const socket = await node.tcp.connect('localhost', 5000)
- // |
- // + node.driver.connect('localhost', 5000)
- for await(const buffer of socket) {
- // process
- }
- // ---------------------------------------------------
- // LAYER: 2: APPLICATION PROTOCOLS
- //
- // HTTP and MEDIA application level functionality.
- // This level builds on top on the RTCDataChannel
- // exposes
- // ---------------------------------------------------
- const server = node.http.listen(80)
- for await(const [req, res] of server) {
- const buffers = []
- for await(const buffer of req) {
- buffers.push(buffer)
- }
- await res.header(200, { })
- await res.write('hello')
- await res.end()
- }
- // upgrade to web socket
- for await(const [req, res] of server) {
- const socket = await req.upgradeToSocket()
- socket.send('1234')
- for await (const message of socket) {
- ///
- }
- }
- // ... later
- server.dispose()
- // |
- // + --- net: server.dispose()
- // |
- // + ---- driver: listener.dispose()
- const data = await node.http.fetch('http://localhost:80').then(res => res.text())
- // ---------------------------------------------------
- // mediastream
- // ---------------------------------------------------
- const mediastream = node.media.pattern()
- for await(const client of node.media.listen(1000)) {
- await client.send(mediastream)
- await client.end()
- }
- const client = node.media.connect('localhost', 5000)
- const mediastream = await client.receive()
- await client.end()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement