Integration: Add mplex to JS side

Relates to #5
This commit is contained in:
backkem 2019-02-14 16:25:21 +01:00 committed by Michiel De Backker
parent 76a6d3027d
commit 40fc80ae24
4 changed files with 73 additions and 46 deletions

View File

@ -1,27 +0,0 @@
const WebRTCDirect = require('libp2p-webrtc-direct')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const direct = new WebRTCDirect()
direct.dial(mh, (err, conn) => {
if(err){
console.log(`Error: ${err}`)
}
console.log(`dial success`)
pull(
conn,
pull.collect((err, values) => {
if (!err) {
console.log(`Value: ${values.toString()}`)
} else {
console.log(`Error: ${err}`)
}
}),
)
})

55
integration/index.js Normal file
View File

@ -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
)
})
}

View File

@ -1,19 +0,0 @@
const WebRTCDirect = require('libp2p-webrtc-direct')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const direct = new WebRTCDirect()
const listener = direct.createListener({config:{}},(socket) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
socket
)
})
listener.listen(mh, () => {
console.log('listening')
})

18
integration/package.json Normal file
View File

@ -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"
}