mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-25 17:22:30 +00:00
chore: update README
This commit is contained in:
parent
5c8c6afea6
commit
b977a59fb8
38
README.md
38
README.md
@ -13,7 +13,7 @@
|
||||
[](https://github.com/libp2p/interface-connection)
|
||||
|
||||
|
||||
> JavaScript implementation of the TCP module for libp2p. It exposes the [interface-transport](https://github.com/libp2p/interface-connection) for dial/listen. `libp2p-tcp` is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
||||
> JavaScript implementation of the TCP module for libp2p. It exposes the [interface-transport](https://github.com/libp2p/interface-connection) for dial/listen. `libp2p-tcp` is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other transports.
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
@ -41,7 +41,8 @@
|
||||
```js
|
||||
const TCP = require('libp2p-tcp')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
const pipe = require('it-pipe')
|
||||
const { collect } = require('streaming-iterables')
|
||||
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||
|
||||
@ -49,29 +50,24 @@ const tcp = new TCP()
|
||||
|
||||
const listener = tcp.createListener((socket) => {
|
||||
console.log('new connection opened')
|
||||
pull(
|
||||
pull.values(['hello']),
|
||||
pipe(
|
||||
['hello'],
|
||||
socket
|
||||
)
|
||||
})
|
||||
|
||||
listener.listen(mh, () => {
|
||||
console.log('listening')
|
||||
await listener.listen(mh)
|
||||
console.log('listening')
|
||||
|
||||
pull(
|
||||
tcp.dial(mh),
|
||||
pull.collect((err, values) => {
|
||||
if (!err) {
|
||||
console.log(`Value: ${values.toString()}`)
|
||||
} else {
|
||||
console.log(`Error: ${err}`)
|
||||
}
|
||||
const socket = await tcp.dial(mh)
|
||||
const values = await pipe(
|
||||
socket,
|
||||
collect
|
||||
)
|
||||
console.log(`Value: ${values.toString()}`)
|
||||
|
||||
// Close connection after reading
|
||||
listener.close()
|
||||
}),
|
||||
)
|
||||
})
|
||||
// Close connection after reading
|
||||
await listener.close()
|
||||
```
|
||||
|
||||
Outputs:
|
||||
@ -88,12 +84,12 @@ Value: hello
|
||||
|
||||
[](https://github.com/libp2p/interface-transport)
|
||||
|
||||
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
|
||||
`libp2p-tcp` accepts TCP addresses as both IPFS and non IPFS encapsulated addresses, i.e:
|
||||
|
||||
`/ip4/127.0.0.1/tcp/4001`
|
||||
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
|
||||
|
||||
Both for dialing and listening.
|
||||
(both for dialing and listening)
|
||||
|
||||
### Connection
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user