Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const options = {
host: '127.0.0.1',
localAddress: '127.0.0.1',
reuseAddress: true,
// connectTimeout: 5000,
// localPort: 20000,
// interface: "wifi",
};
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -370,6 +372,7 @@ Here are listed all methods implemented in `react-native-tcp-socket` that imitat
| `host` | `<string>` | ✅ | ✅ | Host the socket should connect to. IP address in IPv4 format or `'localhost'`. **Default**: `'localhost'`. |
| `localAddress` | `<string>` | ✅ | ✅ | 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` | `<number>` | ✅ | ✅ | Local port the socket should connect from. If not specified, the OS will decide. |
| `connectTimeout` | `<number>` | ✅ | ✅ | 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` | `<string>` | ❌ | ✅ | Interface the socket should connect from. If not specified, it will use the current active connection. The options are: `'wifi', 'ethernet', 'cellular'`. |
| `reuseAddress` | `<boolean>` | ❌ | ✅ | Enable/disable the reuseAddress socket option. **Default**: `true`. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions ios/TcpSocketClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 : @""];
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions lib/types/Socket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export type ConnectionOptions = {
tls?: boolean | undefined;
tlsCheckValidity?: boolean | undefined;
tlsCert?: any;
connectTimeout?: number;
};
export type ReadableEvents = {
pause: () => void;
Expand Down
1 change: 1 addition & 0 deletions src/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { nativeEventEmitter, getNextId } from './Globals';
* tls?: boolean,
* tlsCheckValidity?: boolean,
* tlsCert?: any,
* connectTimeout?: number,
* }} ConnectionOptions
*
* @typedef {object} ReadableEvents
Expand Down