docs: added example to README (#80)

The example is adapted from the example in js-libp2p-tcp
This commit is contained in:
nikor 2019-01-08 21:30:54 +01:00 committed by Jacob Heun
parent d661bb6e45
commit c26ae32829

View File

@ -36,8 +36,40 @@
### Example
```
TODO
```js
const WS = require('libp2p-websockets')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090/ws')
const ws = new WS()
const listener = ws.createListener((socket) => {
console.log('new connection opened')
pull(
pull.values(['hello']),
socket
)
})
listener.listen(mh, () => {
console.log('listening')
pull(
ws.dial(mh),
pull.collect((err, values) => {
if (!err) {
console.log(`Value: ${values.toString()}`)
} else {
console.log(`Error: ${err}`)
}
// Close connection after reading
listener.close()
}),
)
})
```
## API