From f5fe68608b52ceb70ea6b7a4f197ee830d89c98d Mon Sep 17 00:00:00 2001 From: Mao Date: Wed, 31 Dec 2025 15:01:14 +0800 Subject: [PATCH 1/3] feat: support connectTimeout --- .../java/com/asterinet/react/tcpsocket/TcpSocketClient.java | 3 ++- ios/TcpSocketClient.m | 5 +++-- lib/types/Socket.d.ts | 1 + src/Socket.js | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java index 6ef8269..feff1e1 100644 --- a/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java +++ b/android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java @@ -65,7 +65,8 @@ public void connect(Context context, String address, final Integer port, Readabl final int localPort = options.hasKey("localPort") ? options.getInt("localPort") : 0; // bind socket.bind(new InetSocketAddress(localInetAddress, localPort)); - socket.connect(new InetSocketAddress(remoteInetAddress, port)); + final int connectTimeout = options.hasKey("connectTimeout") ? options.getInt("connectTimeout") : 0; + socket.connect(new InetSocketAddress(remoteInetAddress, port), connectTimeout); if (socket instanceof SSLSocket) ((SSLSocket) socket).startHandshake(); startListening(); } diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index ad52d6c..6d41f0a 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -144,11 +144,12 @@ - (BOOL)connect:(NSString *)host NSString *localAddress = options[@"localAddress"]; NSNumber *localPort = options[@"localPort"]; + int connectTimeout = options[@"connectTimeout"] ? [options[@"connectTimeout"] intValue] / 1000 : -1; _host = host; _connecting = true; if (!localAddress && !localPort) { - result = [_tcpSocket connectToHost:host onPort:port error:error]; + result = [_tcpSocket connectToHost:host onPort:port withTimeout:connectTimeout error:error]; } else { NSMutableArray *interface = [NSMutableArray arrayWithCapacity:2]; [interface addObject:localAddress ? localAddress : @""]; @@ -159,7 +160,7 @@ - (BOOL)connect:(NSString *)host [_tcpSocket connectToHost:host onPort:port viaInterface:[interface componentsJoinedByString:@":"] - withTimeout:-1 + withTimeout:connectTimeout error:error]; } if (result && tlsOptions) { diff --git a/lib/types/Socket.d.ts b/lib/types/Socket.d.ts index 92cbd46..e722f3a 100644 --- a/lib/types/Socket.d.ts +++ b/lib/types/Socket.d.ts @@ -253,6 +253,7 @@ export type ConnectionOptions = { tls?: boolean | undefined; tlsCheckValidity?: boolean | undefined; tlsCert?: any; + connectTimeout?: number; }; export type ReadableEvents = { pause: () => void; diff --git a/src/Socket.js b/src/Socket.js index 6ed2141..2619b66 100644 --- a/src/Socket.js +++ b/src/Socket.js @@ -25,6 +25,7 @@ import { nativeEventEmitter, getNextId } from './Globals'; * tls?: boolean, * tlsCheckValidity?: boolean, * tlsCert?: any, + * connectTimeout?: number, * }} ConnectionOptions * * @typedef {object} ReadableEvents From 211618348acba6f9a6c9a3267ad42c17b69abd72 Mon Sep 17 00:00:00 2001 From: Mao Date: Fri, 16 Jan 2026 01:19:51 +0800 Subject: [PATCH 2/3] doc: supplement the connectTimeout documentation --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 5998724..b05e484 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,7 @@ const options = { host: '127.0.0.1', localAddress: '127.0.0.1', reuseAddress: true, + // connectTimeout: 5000, // localPort: 20000, // interface: "wifi", }; @@ -250,6 +251,7 @@ const options = { host: '127.0.0.1', localAddress: '127.0.0.1', reuseAddress: true, + // connectTimeout: 5000, // localPort: 20000, // interface: "wifi", ca: require('server-cert.pem'), @@ -370,6 +372,7 @@ Here are listed all methods implemented in `react-native-tcp-socket` that imitat | `host` | `` | ✅ | ✅ | Host the socket should connect to. IP address in IPv4 format or `'localhost'`. **Default**: `'localhost'`. | | `localAddress` | `` | ✅ | ✅ | Local address the socket should connect from. If not specified, the OS will decide. It is **highly recommended** to specify a `localAddress` to prevent overload errors and improve performance. | | `localPort` | `` | ✅ | ✅ | Local port the socket should connect from. If not specified, the OS will decide. | +| `connectTimeout` | `` | ✅ | ✅ | Connects the socket to a server with a configurable connection timeout (in milliseconds).
If the timeout expires before the connection is established, the operation fails.
When no timeout is specified, the connection will block indefinitely until it either succeeds or an error occurs. | | `interface` | `` | ❌ | ✅ | Interface the socket should connect from. If not specified, it will use the current active connection. The options are: `'wifi', 'ethernet', 'cellular'`. | | `reuseAddress` | `` | ❌ | ✅ | Enable/disable the reuseAddress socket option. **Default**: `true`. | From 98087b65bca8085eb97ae6c4ed60e4618cab8f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Mart=C3=ADn?= Date: Thu, 15 Jan 2026 20:30:53 +0100 Subject: [PATCH 3/3] Improve connectTimeout description formatting Removed HTML line breaks from the connectTimeout description for better readability. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b05e484..ed721f4 100644 --- a/README.md +++ b/README.md @@ -372,7 +372,7 @@ Here are listed all methods implemented in `react-native-tcp-socket` that imitat | `host` | `` | ✅ | ✅ | Host the socket should connect to. IP address in IPv4 format or `'localhost'`. **Default**: `'localhost'`. | | `localAddress` | `` | ✅ | ✅ | Local address the socket should connect from. If not specified, the OS will decide. It is **highly recommended** to specify a `localAddress` to prevent overload errors and improve performance. | | `localPort` | `` | ✅ | ✅ | Local port the socket should connect from. If not specified, the OS will decide. | -| `connectTimeout` | `` | ✅ | ✅ | Connects the socket to a server with a configurable connection timeout (in milliseconds).
If the timeout expires before the connection is established, the operation fails.
When no timeout is specified, the connection will block indefinitely until it either succeeds or an error occurs. | +| `connectTimeout` | `` | ✅ | ✅ | Connects the socket to a server with a configurable connection timeout (in milliseconds). If the timeout expires before the connection is established, the operation fails. When no timeout is specified, the connection will block indefinitely until it either succeeds or an error occurs. | | `interface` | `` | ❌ | ✅ | Interface the socket should connect from. If not specified, it will use the current active connection. The options are: `'wifi', 'ethernet', 'cellular'`. | | `reuseAddress` | `` | ❌ | ✅ | Enable/disable the reuseAddress socket option. **Default**: `true`. |