Skip to content

Commit f5fe686

Browse files
committed
feat: support connectTimeout
1 parent 4c6d55e commit f5fe686

4 files changed

Lines changed: 7 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public void connect(Context context, String address, final Integer port, Readabl
6565
final int localPort = options.hasKey("localPort") ? options.getInt("localPort") : 0;
6666
// bind
6767
socket.bind(new InetSocketAddress(localInetAddress, localPort));
68-
socket.connect(new InetSocketAddress(remoteInetAddress, port));
68+
final int connectTimeout = options.hasKey("connectTimeout") ? options.getInt("connectTimeout") : 0;
69+
socket.connect(new InetSocketAddress(remoteInetAddress, port), connectTimeout);
6970
if (socket instanceof SSLSocket) ((SSLSocket) socket).startHandshake();
7071
startListening();
7172
}

ios/TcpSocketClient.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ - (BOOL)connect:(NSString *)host
144144

145145
NSString *localAddress = options[@"localAddress"];
146146
NSNumber *localPort = options[@"localPort"];
147+
int connectTimeout = options[@"connectTimeout"] ? [options[@"connectTimeout"] intValue] / 1000 : -1;
147148

148149
_host = host;
149150
_connecting = true;
150151
if (!localAddress && !localPort) {
151-
result = [_tcpSocket connectToHost:host onPort:port error:error];
152+
result = [_tcpSocket connectToHost:host onPort:port withTimeout:connectTimeout error:error];
152153
} else {
153154
NSMutableArray *interface = [NSMutableArray arrayWithCapacity:2];
154155
[interface addObject:localAddress ? localAddress : @""];
@@ -159,7 +160,7 @@ - (BOOL)connect:(NSString *)host
159160
[_tcpSocket connectToHost:host
160161
onPort:port
161162
viaInterface:[interface componentsJoinedByString:@":"]
162-
withTimeout:-1
163+
withTimeout:connectTimeout
163164
error:error];
164165
}
165166
if (result && tlsOptions) {

lib/types/Socket.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export type ConnectionOptions = {
253253
tls?: boolean | undefined;
254254
tlsCheckValidity?: boolean | undefined;
255255
tlsCert?: any;
256+
connectTimeout?: number;
256257
};
257258
export type ReadableEvents = {
258259
pause: () => void;

src/Socket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { nativeEventEmitter, getNextId } from './Globals';
2525
* tls?: boolean,
2626
* tlsCheckValidity?: boolean,
2727
* tlsCert?: any,
28+
* connectTimeout?: number,
2829
* }} ConnectionOptions
2930
*
3031
* @typedef {object} ReadableEvents

0 commit comments

Comments
 (0)