diff --git a/README.md b/README.md index 5998724..ed721f4 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`. | 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