mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-25 08:12:33 +00:00
docs: fixing the broken example in README (#91)
* Working example fix * Char fix * Corrected output, and add yarn to gitignore
This commit is contained in:
parent
df0aa059ca
commit
5ef24695fc
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,6 +4,9 @@ docs
|
|||||||
test/repo-tests*
|
test/repo-tests*
|
||||||
**/bundle.js
|
**/bundle.js
|
||||||
|
|
||||||
|
# yarn
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
|
25
README.md
25
README.md
@ -39,12 +39,11 @@ const TCP = require('libp2p-tcp')
|
|||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
|
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
const mh2 = multiaddr('/ip6/::/tcp/9092')
|
|
||||||
|
|
||||||
const tcp = new TCP()
|
const tcp = new TCP()
|
||||||
|
|
||||||
const listener = tcp.createListener(mh1, (socket) => {
|
const listener = tcp.createListener((socket) => {
|
||||||
console.log('new connection opened')
|
console.log('new connection opened')
|
||||||
pull(
|
pull(
|
||||||
pull.values(['hello']),
|
pull.values(['hello']),
|
||||||
@ -52,15 +51,21 @@ const listener = tcp.createListener(mh1, (socket) => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
listener.listen(() => {
|
listener.listen(mh, () => {
|
||||||
console.log('listening')
|
console.log('listening')
|
||||||
|
|
||||||
pull(
|
pull(
|
||||||
tcp.dial(mh1),
|
tcp.dial(mh),
|
||||||
pull.log,
|
pull.collect((err, values) => {
|
||||||
pull.onEnd(() => {
|
if (!err) {
|
||||||
tcp.close()
|
console.log(`Value: ${values.toString()}`)
|
||||||
})
|
} else {
|
||||||
|
console.log(`Error: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close connection after reading
|
||||||
|
listener.close()
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -70,7 +75,7 @@ Outputs:
|
|||||||
```sh
|
```sh
|
||||||
listening
|
listening
|
||||||
new connection opened
|
new connection opened
|
||||||
hello
|
Value: hello
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
Loading…
x
Reference in New Issue
Block a user