mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-26 10:22:26 +00:00
chore: update README
This commit is contained in:
parent
5c8c6afea6
commit
b977a59fb8
32
README.md
32
README.md
@ -13,7 +13,7 @@
|
|||||||
[](https://github.com/libp2p/interface-connection)
|
[](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
|
## Lead Maintainer
|
||||||
|
|
||||||
@ -41,7 +41,8 @@
|
|||||||
```js
|
```js
|
||||||
const TCP = require('libp2p-tcp')
|
const TCP = require('libp2p-tcp')
|
||||||
const multiaddr = require('multiaddr')
|
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')
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
@ -49,29 +50,24 @@ const tcp = new TCP()
|
|||||||
|
|
||||||
const listener = tcp.createListener((socket) => {
|
const listener = tcp.createListener((socket) => {
|
||||||
console.log('new connection opened')
|
console.log('new connection opened')
|
||||||
pull(
|
pipe(
|
||||||
pull.values(['hello']),
|
['hello'],
|
||||||
socket
|
socket
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
listener.listen(mh, () => {
|
await listener.listen(mh)
|
||||||
console.log('listening')
|
console.log('listening')
|
||||||
|
|
||||||
pull(
|
const socket = await tcp.dial(mh)
|
||||||
tcp.dial(mh),
|
const values = await pipe(
|
||||||
pull.collect((err, values) => {
|
socket,
|
||||||
if (!err) {
|
collect
|
||||||
|
)
|
||||||
console.log(`Value: ${values.toString()}`)
|
console.log(`Value: ${values.toString()}`)
|
||||||
} else {
|
|
||||||
console.log(`Error: ${err}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close connection after reading
|
// Close connection after reading
|
||||||
listener.close()
|
await listener.close()
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Outputs:
|
Outputs:
|
||||||
@ -88,12 +84,12 @@ Value: hello
|
|||||||
|
|
||||||
[](https://github.com/libp2p/interface-transport)
|
[](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`
|
||||||
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
|
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
|
||||||
|
|
||||||
Both for dialing and listening.
|
(both for dialing and listening)
|
||||||
|
|
||||||
### Connection
|
### Connection
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user