Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit c26ae32

Browse files
nikorjacobheun
authored andcommittedJan 8, 2019
docs: added example to README (#80)
The example is adapted from the example in js-libp2p-tcp
1 parent d661bb6 commit c26ae32

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed
 

‎README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,40 @@
3636

3737
### Example
3838

39-
```
40-
TODO
39+
```js
40+
const WS = require('libp2p-websockets')
41+
const multiaddr = require('multiaddr')
42+
const pull = require('pull-stream')
43+
44+
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090/ws')
45+
46+
const ws = new WS()
47+
48+
const listener = ws.createListener((socket) => {
49+
console.log('new connection opened')
50+
pull(
51+
pull.values(['hello']),
52+
socket
53+
)
54+
})
55+
56+
listener.listen(mh, () => {
57+
console.log('listening')
58+
59+
pull(
60+
ws.dial(mh),
61+
pull.collect((err, values) => {
62+
if (!err) {
63+
console.log(`Value: ${values.toString()}`)
64+
} else {
65+
console.log(`Error: ${err}`)
66+
}
67+
68+
// Close connection after reading
69+
listener.close()
70+
}),
71+
)
72+
})
4173
```
4274

4375
## API

0 commit comments

Comments
 (0)
This repository has been archived.