diff --git a/integration/dial.js b/integration/dial.js deleted file mode 100644 index b16effc..0000000 --- a/integration/dial.js +++ /dev/null @@ -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}`) - } - }), - ) - }) \ No newline at end of file diff --git a/integration/index.js b/integration/index.js new file mode 100644 index 0000000..e99c80a --- /dev/null +++ b/integration/index.js @@ -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 + ) + }) +} diff --git a/integration/listen.js b/integration/listen.js deleted file mode 100644 index 9068077..0000000 --- a/integration/listen.js +++ /dev/null @@ -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') -}) \ No newline at end of file diff --git a/integration/package.json b/integration/package.json new file mode 100644 index 0000000..09d8163 --- /dev/null +++ b/integration/package.json @@ -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 ", + "license": "MIT" +}