Skip to content

Commit 1658831

Browse files
authored
feat: Add socket address properties (#94)
Add remotePort, remoteAddress, remoteFamily, localPort, localAddress properties in TcpSocket. BREAKING CHANGE: Events types and callbacks now match NodeJS official API.
1 parent 783bd80 commit 1658831

26 files changed

+1147
-529
lines changed

.eslintrc.json

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"parser": "babel-eslint",
3-
"plugins": ["prettier", "jest", "jsdoc"],
3+
"plugins": ["prettier", "jest"],
44
"env": {
55
"es6": true,
66
"node": true
@@ -13,8 +13,7 @@
1313
"eslint:recommended",
1414
"prettier",
1515
"plugin:jest/recommended",
16-
"plugin:jest/style",
17-
"plugin:jsdoc/recommended"
16+
"plugin:jest/style"
1817
],
1918
"rules": {
2019
"prefer-const": [
@@ -28,10 +27,6 @@
2827
"no-var": "error",
2928
"prefer-template": 2,
3029
"require-atomic-updates": "off",
31-
"prettier/prettier": ["error"],
32-
"jsdoc/require-returns-description": 0,
33-
"jsdoc/require-param-description": 0,
34-
"jsdoc/no-undefined-types": 0,
35-
"jsdoc/require-returns": 0
30+
"prettier/prettier": ["error"]
3631
}
3732
}

README.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-native-tcp-socket
1+
# react-native-tcp-socket <!-- omit in toc -->
22
<p align="center">
33
<img src="https://github.com/Rapsssito/react-native-tcp-socket/workflows/tests/badge.svg" />
44
<img src="https://img.shields.io/npm/dw/react-native-tcp-socket" />
@@ -7,15 +7,22 @@
77

88
React Native TCP socket API for Android & iOS with **client SSL/TLS support**. It allows you to create TCP clients and servers sockets, imitating some of Node's [net](https://nodejs.org/api/net.html) API functionalities (check the available [API](#api) for more information).
99

10-
## Table of Contents
10+
## Table of Contents <!-- omit in toc -->
1111

1212
- [Getting started](#getting-started)
13-
- [Self-Signed SSL](#self-signed-ssl-only-available-for-react-native--060)
14-
- [Compatibility](#react-native-compatibility)
13+
- [Using React Native >= 0.60](#using-react-native--060)
14+
- [Self-Signed SSL (only available for React Native > 0.60)](#self-signed-ssl-only-available-for-react-native--060)
15+
- [Using React Native < 0.60](#using-react-native--060-1)
16+
- [React Native Compatibility](#react-native-compatibility)
1517
- [Usage](#usage)
18+
- [Client](#client)
19+
- [Server](#server)
20+
- [SSL Client](#ssl-client)
1621
- [API](#api)
1722
- [TcpSocket](#tcpsocket)
23+
- [`createConnection()`](#createconnection)
1824
- [TcpServer](#tcpserver)
25+
- [`listen()`](#listen)
1926
- [Maintainers](#maintainers)
2027
- [Acknowledgments](#acknowledgments)
2128
- [License](#license)
@@ -195,6 +202,12 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func
195202
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
196203
* [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback)
197204
* [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback)
205+
* **Properties:**
206+
* [`'remoteAddress'`](https://nodejs.org/api/net.html#net_socket_remoteaddress)
207+
* [`'remoteFamily'`](https://nodejs.org/api/net.html#net_socket_remotefamily)
208+
* [`'remotePort'`](https://nodejs.org/api/net.html#net_socket_remoteport)
209+
* [`'localAddress'`](https://nodejs.org/api/net.html#net_socket_localaddress)
210+
* [`'localPort'`](https://nodejs.org/api/net.html#net_socket_localport)
198211
* **Events:**
199212
* [`'close'`](https://nodejs.org/api/net.html#net_event_close_1)
200213
* [`'connect'`](https://nodejs.org/api/net.html#net_event_connect)
@@ -203,7 +216,7 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func
203216

204217
#### `createConnection()`
205218
`createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options).
206-
##### `createConnection: options`
219+
##### `createConnection: options` <!-- omit in toc -->
207220
**Required**. Available options for creating a socket. It must be an `object` with the following properties:
208221

209222
| Property | Type | iOS | Android |Description |
@@ -226,11 +239,16 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func
226239
* [`TcpSocket.createServer(connectionListener)`](https://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener)
227240
* **[`listen(options[, callback])`](#listen)**
228241
* [`close([callback])`](https://nodejs.org/api/net.html#net_server_close_callback)
242+
* **Events:**
243+
* [`'close'`](https://nodejs.org/api/net.html#net_event_close)
244+
* [`'connection'`](https://nodejs.org/api/net.html#net_event_connection)
245+
* [`'error'`](https://nodejs.org/api/net.html#net_event_error)
246+
* [`'listening'`](https://nodejs.org/api/net.html#net_event_listening)
229247

230248
#### `listen()`
231249
`listen(options[, callback])` creates a TCP server socket using the given [`options`](#listen-options).
232250

233-
##### `listen: options`
251+
##### `listen: options` <!-- omit in toc -->
234252
**Required**. Available options for creating a server socket. It must be an `object` with the following properties:
235253

236254
| Property | Type | iOS | Android |Description |

android/src/main/java/com/asterinet/react/tcpsocket/TcpReceiverTask.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected final Void doInBackground(Pair<TcpSocketClient, TcpReceiverTask.OnData
3838
if (bufferCount > 0) {
3939
receiverListener.onData(socketId, Arrays.copyOfRange(buffer, 0, bufferCount));
4040
} else if (bufferCount == -1) {
41-
clientSocket.close();
41+
clientSocket.destroy();
4242
}
4343
}
4444
} catch (IOException ioe) {
@@ -55,9 +55,11 @@ protected final Void doInBackground(Pair<TcpSocketClient, TcpReceiverTask.OnData
5555
*/
5656
@SuppressWarnings("WeakerAccess")
5757
public interface OnDataReceivedListener {
58-
void onConnection(Integer serverId, Integer clientId, InetSocketAddress socketAddress);
58+
void onConnection(Integer serverId, Integer clientId, Socket socket);
5959

60-
void onConnect(Integer id, String host, int port);
60+
void onConnect(Integer id, TcpSocketClient client);
61+
62+
void onListen(Integer id, TcpSocketServer server);
6163

6264
void onData(Integer id, byte[] data);
6365

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.asterinet.react.tcpsocket;
2+
3+
public class TcpSocket {
4+
private final int id;
5+
6+
public TcpSocket(final int id) {
7+
this.id = id;
8+
}
9+
10+
public int getId() {
11+
return id;
12+
}
13+
}

android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@
2222
import androidx.annotation.NonNull;
2323
import androidx.annotation.Nullable;
2424

25-
class TcpSocketClient {
26-
private final int id;
25+
class TcpSocketClient extends TcpSocket {
2726
private final ExecutorService executorService;
2827
private TcpReceiverTask receiverTask;
2928
private Socket socket;
3029
private TcpReceiverTask.OnDataReceivedListener mReceiverListener;
3130

32-
TcpSocketClient(final int id) {
33-
this.id = id;
34-
this.executorService = Executors.newFixedThreadPool(1);
35-
}
36-
3731
TcpSocketClient(@NonNull final TcpReceiverTask.OnDataReceivedListener receiverListener, @NonNull final Integer id, @Nullable final Socket socket) {
38-
this(id);
32+
super(id);
33+
this.executorService = Executors.newFixedThreadPool(1);
3934
this.socket = socket;
4035
receiverTask = new TcpReceiverTask();
4136
mReceiverListener = receiverListener;
@@ -45,10 +40,6 @@ ExecutorService getExecutorService() {
4540
return this.executorService;
4641
}
4742

48-
public int getId() {
49-
return id;
50-
}
51-
5243
public Socket getSocket() {
5344
return socket;
5445
}
@@ -114,7 +105,7 @@ public void write(final byte[] data) throws IOException {
114105
/**
115106
* Shuts down the receiver task, closing the socket.
116107
*/
117-
public void close() {
108+
public void destroy() {
118109
try {
119110
if (receiverTask != null && !receiverTask.isCancelled()) {
120111
// stop the receiving task

0 commit comments

Comments
 (0)