js-libp2p-tcp/README.md

97 lines
2.6 KiB
Markdown
Raw Normal View History

2015-10-29 07:27:39 +00:00
js-libp2p-tcp
2015-09-16 12:35:18 +01:00
===============
2016-03-14 16:57:54 +00:00
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://travis-ci.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-tcp)
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-tcp)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
2015-09-16 12:35:18 +01:00
2015-09-16 17:43:45 +01:00
![](https://raw.githubusercontent.com/diasdavid/abstract-connection/master/img/badge.png)
![](https://raw.githubusercontent.com/diasdavid/abstract-transport/master/img/badge.png)
2016-04-21 16:18:39 -07:00
> Node.js implementation of the TCP module that libp2p uses, which implements
> the [interface-connection](https://github.com/diasdavid/interface-connection)
> interface for dial/listen.
2015-09-16 12:35:18 +01:00
2016-04-21 16:18:39 -07:00
## Description
2016-04-22 14:14:07 -07:00
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a
2016-04-21 16:18:39 -07:00
`multiaddr`. This small shim will enable libp2p to use other different
transports.
## Example
```js
2016-04-21 16:33:57 -07:00
const Tcp = require('libp2p-tcp')
const multiaddr = require('multiaddr')
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
const mh2 = multiaddr('/ip6/::/tcp/9092')
const tcp = new Tcp()
tcp.createListener([mh1, mh2], function handler (socket) {
console.log('connection')
socket.end('bye')
}, function ready () {
console.log('ready')
const client = tcp.dial(mh1)
client.pipe(process.stdout)
client.on('end', () => {
2016-04-22 14:14:07 -07:00
tcp.close()
2016-04-21 16:33:57 -07:00
})
})
2016-04-21 16:18:39 -07:00
```
outputs
```
2016-04-21 16:33:57 -07:00
ready
connection
bye
2016-04-21 16:18:39 -07:00
```
## Installation
### npm
```sh
> npm i libp2p-tcp
```
## API
```js
const Tcp = require('libp2p-tcp')
```
### var tcp = new Tcp()
2016-04-22 14:14:07 -07:00
Creates a new TCP object. This does nothing on its own, but provides access to
2016-04-21 16:33:57 -07:00
`dial` and `createListener`.
2016-04-21 16:18:39 -07:00
2016-04-22 14:14:07 -07:00
### tcp.createListener(multiaddrs, handler, ready)
2016-04-21 16:18:39 -07:00
2016-04-21 16:33:57 -07:00
Creates TCP servers that listen on the addresses described in the array
`multiaddrs`. Each connection will call `handler` with a connection stream.
2016-04-22 14:14:07 -07:00
`ready` is called once all servers are listening.
2016-04-21 16:18:39 -07:00
2016-04-21 16:33:57 -07:00
### tcp.dial(multiaddr, options={})
2016-04-21 16:18:39 -07:00
2016-04-21 16:33:57 -07:00
Connects to the multiaddress `multiaddr` using TCP, returning the socket stream.
If `options.ready` is set to a function, it is called when a connection is
established.
2016-04-21 16:18:39 -07:00
2016-04-21 16:33:57 -07:00
### tcp.close(callback)
2016-04-21 16:18:39 -07:00
2016-04-21 16:33:57 -07:00
Closes all the listening TCP servers, calling `callback` once all of them have
been shut down.
2016-04-21 16:18:39 -07:00
## License
MIT