Skip to content

feat: Add socket address properties #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"parser": "babel-eslint",
"plugins": ["prettier", "jest", "jsdoc"],
"plugins": ["prettier", "jest"],
"env": {
"es6": true,
"node": true
@@ -13,8 +13,7 @@
"eslint:recommended",
"prettier",
"plugin:jest/recommended",
"plugin:jest/style",
"plugin:jsdoc/recommended"
"plugin:jest/style"
],
"rules": {
"prefer-const": [
@@ -28,10 +27,6 @@
"no-var": "error",
"prefer-template": 2,
"require-atomic-updates": "off",
"prettier/prettier": ["error"],
"jsdoc/require-returns-description": 0,
"jsdoc/require-param-description": 0,
"jsdoc/no-undefined-types": 0,
"jsdoc/require-returns": 0
"prettier/prettier": ["error"]
}
}
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# react-native-tcp-socket
# react-native-tcp-socket <!-- omit in toc -->
<p align="center">
<img src="https://github.com/Rapsssito/react-native-tcp-socket/workflows/tests/badge.svg" />
<img src="https://img.shields.io/npm/dw/react-native-tcp-socket" />
@@ -7,15 +7,22 @@

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).

## Table of Contents
## Table of Contents <!-- omit in toc -->

- [Getting started](#getting-started)
- [Self-Signed SSL](#self-signed-ssl-only-available-for-react-native--060)
- [Compatibility](#react-native-compatibility)
- [Using React Native >= 0.60](#using-react-native--060)
- [Self-Signed SSL (only available for React Native > 0.60)](#self-signed-ssl-only-available-for-react-native--060)
- [Using React Native < 0.60](#using-react-native--060-1)
- [React Native Compatibility](#react-native-compatibility)
- [Usage](#usage)
- [Client](#client)
- [Server](#server)
- [SSL Client](#ssl-client)
- [API](#api)
- [TcpSocket](#tcpsocket)
- [`createConnection()`](#createconnection)
- [TcpServer](#tcpserver)
- [`listen()`](#listen)
- [Maintainers](#maintainers)
- [Acknowledgments](#acknowledgments)
- [License](#license)
@@ -195,6 +202,12 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
* [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback)
* [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback)
* **Properties:**
* [`'remoteAddress'`](https://nodejs.org/api/net.html#net_socket_remoteaddress)
* [`'remoteFamily'`](https://nodejs.org/api/net.html#net_socket_remotefamily)
* [`'remotePort'`](https://nodejs.org/api/net.html#net_socket_remoteport)
* [`'localAddress'`](https://nodejs.org/api/net.html#net_socket_localaddress)
* [`'localPort'`](https://nodejs.org/api/net.html#net_socket_localport)
* **Events:**
* [`'close'`](https://nodejs.org/api/net.html#net_event_close_1)
* [`'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

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

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

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

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

| Property | Type | iOS | Android |Description |
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ protected final Void doInBackground(Pair<TcpSocketClient, TcpReceiverTask.OnData
if (bufferCount > 0) {
receiverListener.onData(socketId, Arrays.copyOfRange(buffer, 0, bufferCount));
} else if (bufferCount == -1) {
clientSocket.close();
clientSocket.destroy();
}
}
} catch (IOException ioe) {
@@ -55,9 +55,11 @@ protected final Void doInBackground(Pair<TcpSocketClient, TcpReceiverTask.OnData
*/
@SuppressWarnings("WeakerAccess")
public interface OnDataReceivedListener {
void onConnection(Integer serverId, Integer clientId, InetSocketAddress socketAddress);
void onConnection(Integer serverId, Integer clientId, Socket socket);

void onConnect(Integer id, String host, int port);
void onConnect(Integer id, TcpSocketClient client);

void onListen(Integer id, TcpSocketServer server);

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

13 changes: 13 additions & 0 deletions android/src/main/java/com/asterinet/react/tcpsocket/TcpSocket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.asterinet.react.tcpsocket;

public class TcpSocket {
private final int id;

public TcpSocket(final int id) {
this.id = id;
}

public int getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -22,20 +22,15 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

class TcpSocketClient {
private final int id;
class TcpSocketClient extends TcpSocket {
private final ExecutorService executorService;
private TcpReceiverTask receiverTask;
private Socket socket;
private TcpReceiverTask.OnDataReceivedListener mReceiverListener;

TcpSocketClient(final int id) {
this.id = id;
this.executorService = Executors.newFixedThreadPool(1);
}

TcpSocketClient(@NonNull final TcpReceiverTask.OnDataReceivedListener receiverListener, @NonNull final Integer id, @Nullable final Socket socket) {
this(id);
super(id);
this.executorService = Executors.newFixedThreadPool(1);
this.socket = socket;
receiverTask = new TcpReceiverTask();
mReceiverListener = receiverListener;
@@ -45,10 +40,6 @@ ExecutorService getExecutorService() {
return this.executorService;
}

public int getId() {
return id;
}

public Socket getSocket() {
return socket;
}
@@ -114,7 +105,7 @@ public void write(final byte[] data) throws IOException {
/**
* Shuts down the receiver task, closing the socket.
*/
public void close() {
public void destroy() {
try {
if (receiverTask != null && !receiverTask.isCancelled()) {
// stop the receiving task
Loading