react-native-tcp/README.md

86 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2015-12-30 00:13:35 +00:00
# TCP in React Native
node's [net](https://nodejs.org/api/net.html) API in React Native
This module is used by [Peel](http://www.peel.com/)
## Install
* Create a new react-native project. [Check react-native getting started](http://facebook.github.io/react-native/docs/getting-started.html#content)
* In your project dir:
2017-01-16 19:34:45 +00:00
```
npm install react-native-tcp --save
```
__Note for iOS:__ If your react-native version < 0.40 install with this tag instead:
2015-12-30 00:13:35 +00:00
```
2017-01-16 19:34:45 +00:00
npm install react-native-tcp@3.1.0 --save
2015-12-30 00:13:35 +00:00
```
## Link in the native dependency
2015-12-30 00:13:35 +00:00
```
2016-09-19 20:03:40 +00:00
react-native link react-native-tcp
```
## Additional dependencies
### Due to limitations in the react-native packager, streams need to be hacked in with [rn-nodeify](https://www.npmjs.com/package/rn-nodeify)
1. install rn-nodeify as a dev-dependency
2017-01-12 00:14:05 +00:00
``` npm install --save-dev rn-nodeify ```
2. run rn-nodeify manually
2017-01-12 00:14:05 +00:00
``` rn-nodeify --install stream,process,util --hack ```
3. optionally you can add this as a postinstall script
2017-01-12 00:14:05 +00:00
``` "postinstall": "rn-nodeify --install stream,process,util --hack" ```
2015-12-30 00:13:35 +00:00
## Usage
### package.json
_only if you want to write require('net') in your javascript_
```json
{
"browser": {
"net": "react-native-tcp"
}
}
```
### JS
2016-09-19 20:03:40 +00:00
_see/run [index.ios.js/index.android.js](examples/rctsockets) for a complete example, but basically it's just like net_
2015-12-30 00:13:35 +00:00
```js
var net = require('net');
// OR, if not shimming via package.json "browser" field:
// var net = require('react-native-tcp')
var server = net.createServer(function(socket) {
2016-09-19 20:03:40 +00:00
socket.write('excellent!');
2015-12-30 00:13:35 +00:00
}).listen(12345);
var client = net.createConnection(12345);
client.on('error', function(error) {
2016-09-19 20:03:40 +00:00
console.log(error)
2015-12-30 00:13:35 +00:00
});
client.on('data', function(data) {
2016-09-19 20:03:40 +00:00
console.log('message was received', data)
2015-12-30 00:13:35 +00:00
});
```
### TODO
add select tests from node's tests for net
PR's welcome!
2016-01-02 19:03:35 +00:00
_originally forked from [react-native-udp](https://github.com/tradle/react-native-udp)_