mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-05-03 22:02:26 +00:00
docs: added example to README (#80)
The example is adapted from the example in js-libp2p-tcp
This commit is contained in:
parent
d661bb6e45
commit
c26ae32829
36
README.md
36
README.md
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user