Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Integration: Add mplex to JS side
Browse files Browse the repository at this point in the history
Relates to #5
  • Loading branch information
backkem committed Feb 14, 2019
1 parent 76a6d30 commit 40fc80a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 46 deletions.
27 changes: 0 additions & 27 deletions integration/dial.js

This file was deleted.

55 changes: 55 additions & 0 deletions integration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const flags = require('flags')
const WebRTCDirect = require('libp2p-webrtc-direct')
const multiaddr = require('multiaddr')
const mplex = require('libp2p-mplex')
const pull = require('pull-stream')

const listenFlag = 'listen'
flags.defineBoolean(listenFlag, false, 'Listen for incoming connections.')
flags.parse()
const listening = flags.get(listenFlag)

const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')

const direct = new WebRTCDirect()

if (listening) {
const listener = direct.createListener({ config: {} }, (conn) => {
console.log('[listener] Got connection')

const muxer = mplex.listener(conn)

muxer.on('stream', (stream) => {
console.log('[listener] Got stream')
pull(
stream,
pull.drain((data) => {
console.log('[listener] Received:')
console.log(data.toString())
})
)
})
})

listener.listen(mh, () => {
console.log('[listener] Listening')
})
} else {
direct.dial(mh, { config: {} }, (err, conn) => {
if (err) {
console.log(`[dialer] Failed to open connection: ${err}`)
}
console.log('[dialer] Opened connection')

const muxer = mplex.dialer(conn)
const stream = muxer.newStream((err) => {
console.log('[dialer] Opened stream')
if (err) throw err
})

pull(
pull.values(['hey, how is it going. I am the dialer']),
stream
)
})
}
19 changes: 0 additions & 19 deletions integration/listen.js

This file was deleted.

18 changes: 18 additions & 0 deletions integration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "integration",
"version": "0.0.1",
"description": "",
"main": "index.js",
"dependencies": {
"flags": "^0.1.3",
"libp2p-mplex": "^0.8.4",
"libp2p-webrtc-direct": "^0.3.1",
"multiaddr": "^6.0.4"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Michiel De Backker <mail@backkem.me>",
"license": "MIT"
}

0 comments on commit 40fc80a

Please sign in to comment.