@react-native-ohos/react-native-tcp-socket 6.3.1-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.OpenSource +11 -0
- package/README.md +13 -0
- package/harmony/tcp_socket/BuildProfile.ets +17 -0
- package/harmony/tcp_socket/Index.ets +6 -0
- package/harmony/tcp_socket/build-profile.json5 +31 -0
- package/harmony/tcp_socket/consumer-rules.txt +0 -0
- package/harmony/tcp_socket/hvigorfile.ts +6 -0
- package/harmony/tcp_socket/obfuscation-rules.txt +23 -0
- package/harmony/tcp_socket/oh-package.json5 +11 -0
- package/harmony/tcp_socket/src/main/cpp/CMakeLists.txt +9 -0
- package/harmony/tcp_socket/src/main/cpp/TcpSocketPackage.h +19 -0
- package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/BaseReactNativeTcpSocketPackage.h +72 -0
- package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.cpp +28 -0
- package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.h +16 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ComponentDescriptors.h +24 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.cpp +16 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.h +17 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.cpp +19 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.h +18 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.cpp +17 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.h +23 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.cpp +16 -0
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.h +19 -0
- package/harmony/tcp_socket/src/main/ets/Logger.ts +46 -0
- package/harmony/tcp_socket/src/main/ets/TcpEventListener.ts +121 -0
- package/harmony/tcp_socket/src/main/ets/TcpSocket.ts +17 -0
- package/harmony/tcp_socket/src/main/ets/TcpSocketClient.ts +444 -0
- package/harmony/tcp_socket/src/main/ets/TcpSocketPackage.ts +27 -0
- package/harmony/tcp_socket/src/main/ets/TcpSocketServer.ts +151 -0
- package/harmony/tcp_socket/src/main/ets/TcpSocketTurboModule.ts +224 -0
- package/harmony/tcp_socket/src/main/ets/generated/components/ts.ts +5 -0
- package/harmony/tcp_socket/src/main/ets/generated/index.ets +5 -0
- package/harmony/tcp_socket/src/main/ets/generated/ts.ts +6 -0
- package/harmony/tcp_socket/src/main/ets/generated/turboModules/TcpSocketModule.ts +38 -0
- package/harmony/tcp_socket/src/main/ets/generated/turboModules/ts.ts +5 -0
- package/harmony/tcp_socket/src/main/module.json5 +11 -0
- package/harmony/tcp_socket/src/main/resources/base/element/string.json +8 -0
- package/harmony/tcp_socket/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/tcp_socket/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/tcp_socket/src/ohosTest/ets/test/Ability.test.ets +35 -0
- package/harmony/tcp_socket/src/ohosTest/ets/test/List.test.ets +5 -0
- package/harmony/tcp_socket/src/ohosTest/module.json5 +13 -0
- package/harmony/tcp_socket/src/test/List.test.ets +5 -0
- package/harmony/tcp_socket/src/test/LocalUnit.test.ets +33 -0
- package/harmony/tcp_socket/ts.ts +7 -0
- package/harmony/tcp_socket.har +0 -0
- package/lib/types/Globals.d.ts +2 -0
- package/lib/types/Server.d.ts +138 -0
- package/lib/types/Socket.d.ts +272 -0
- package/lib/types/TLSServer.d.ts +28 -0
- package/lib/types/TLSSocket.d.ts +50 -0
- package/lib/types/index.d.ts +81 -0
- package/package.json +77 -0
- package/src/Globals.js +12 -0
- package/src/Server.js +271 -0
- package/src/Socket.js +513 -0
- package/src/TLSServer.js +70 -0
- package/src/TLSSocket.js +93 -0
- package/src/TcpSocketModule.ts +40 -0
- package/src/index.js +145 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
import { TurboModule, RNOHError, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
7
|
+
import { TM } from "./generated/ts"
|
|
8
|
+
import { TcpEventListener } from "./TcpEventListener"
|
|
9
|
+
import { connection } from '@kit.NetworkKit';
|
|
10
|
+
import { BusinessError } from '@kit.BasicServicesKit';
|
|
11
|
+
import { TcpSocket } from './TcpSocket'
|
|
12
|
+
import Logger from './Logger'
|
|
13
|
+
import { TcpSocketClient } from './TcpSocketClient'
|
|
14
|
+
import { TcpSocketServer } from './TcpSocketServer'
|
|
15
|
+
import { util } from '@kit.ArkTS';
|
|
16
|
+
import { Context } from '@kit.AbilityKit';
|
|
17
|
+
|
|
18
|
+
export class TcpSocketTurboModule extends TurboModule implements TM.TcpSocketModule.Spec {
|
|
19
|
+
private socketMap: Map<number, TcpSocket> = new Map<number, TcpSocket>();
|
|
20
|
+
private pendingTLS: Map<number, Object> = new Map<number, Object>();
|
|
21
|
+
private mNetworkMap: Map<string, connection.ConnectionProperties> =
|
|
22
|
+
new Map<string, connection.ConnectionProperties>();
|
|
23
|
+
private tcpEvtListener?: TcpEventListener;
|
|
24
|
+
private currentNetwork?: connection.ConnectionProperties;
|
|
25
|
+
private context: Context;
|
|
26
|
+
|
|
27
|
+
constructor(ctx: TurboModuleContext) {
|
|
28
|
+
super(ctx)
|
|
29
|
+
this.tcpEvtListener = new TcpEventListener(ctx);
|
|
30
|
+
this.context = ctx.uiAbilityContext
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private getTcpClient(cid: number): TcpSocketClient | undefined {
|
|
34
|
+
let socket: TcpSocket | undefined = this.socketMap.get(cid);
|
|
35
|
+
if (socket && (socket instanceof TcpSocketClient)) {
|
|
36
|
+
return socket as TcpSocketClient;
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private getTcpServer(cid: number): TcpSocketServer | undefined {
|
|
42
|
+
let socket: TcpSocket | undefined = this.socketMap.get(cid);
|
|
43
|
+
if (socket && (socket instanceof TcpSocketServer)) {
|
|
44
|
+
return socket as TcpSocketServer;
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private async requestNetwork(transportType: number, iotDeviceHost: string): Promise<void> {
|
|
50
|
+
if (!iotDeviceHost || "localhost" === iotDeviceHost) {
|
|
51
|
+
this.getDefaultNetInfo();
|
|
52
|
+
} else {
|
|
53
|
+
let netHandles = connection.getAllNetsSync();
|
|
54
|
+
for (const netHandle of netHandles) {
|
|
55
|
+
let conpro = connection.getNetCapabilitiesSync(netHandle);
|
|
56
|
+
if (conpro.bearerTypes.includes(transportType)) {
|
|
57
|
+
await connection.setAppNet(netHandle, (error: BusinessError, data: void) => {
|
|
58
|
+
if (error) {
|
|
59
|
+
Logger.error(`setAppNet error. Code:${error.code}, message:${error.message}`);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
Logger.info("setAppNet Succeeded");
|
|
63
|
+
this.currentNetwork = connection.getConnectionPropertiesSync(netHandle);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private getDefaultNetInfo() {
|
|
71
|
+
if (!connection.hasDefaultNetSync()) {
|
|
72
|
+
Logger.info("has not default net")
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
let netHandle: connection.NetHandle = connection.getDefaultNetSync();
|
|
76
|
+
this.currentNetwork = connection.getConnectionPropertiesSync(netHandle);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private async selectNetwork(ipAddress: string, iface: string, iotDeviceHost: string): Promise<void> {
|
|
80
|
+
if (!iface) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (ipAddress) {
|
|
84
|
+
let cachedNetwork = this.mNetworkMap.get(iface + ipAddress);
|
|
85
|
+
if (cachedNetwork) {
|
|
86
|
+
this.currentNetwork = cachedNetwork;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
switch (iface) {
|
|
90
|
+
case 'wifi':
|
|
91
|
+
await this.requestNetwork(connection.NetBearType.BEARER_WIFI, iotDeviceHost);
|
|
92
|
+
break;
|
|
93
|
+
case 'cellular':
|
|
94
|
+
await this.requestNetwork(connection.NetBearType.BEARER_CELLULAR, iotDeviceHost);
|
|
95
|
+
break;
|
|
96
|
+
case 'ethernet':
|
|
97
|
+
await this.requestNetwork(connection.NetBearType.BEARER_ETHERNET, iotDeviceHost);
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (!this.currentNetwork) {
|
|
101
|
+
throw new Error("Interface " + iface + " unreachable");
|
|
102
|
+
} else {
|
|
103
|
+
this.mNetworkMap.set(iface + ipAddress, this.currentNetwork);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async connect(cId: number, host: string, port: number, options: Object): Promise<void> {
|
|
108
|
+
if (this.socketMap.get(cId)) {
|
|
109
|
+
this.tcpEvtListener?.onError(cId, "connect() called twice with the same id.");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
let localAddress: string = options['localAddress'];
|
|
114
|
+
let iface: string = options['interface'];
|
|
115
|
+
let iotDeviceHost: string = options['host'];
|
|
116
|
+
this.selectNetwork(localAddress, iface, iotDeviceHost);
|
|
117
|
+
let client: TcpSocketClient = new TcpSocketClient(this.tcpEvtListener, cId);
|
|
118
|
+
this.socketMap.set(cId, client);
|
|
119
|
+
let tlsOptions = this.pendingTLS.get(cId);
|
|
120
|
+
await client.connect(host, port, options, tlsOptions);
|
|
121
|
+
this.tcpEvtListener?.onConnect(cId, client);
|
|
122
|
+
} catch (err) {
|
|
123
|
+
this.tcpEvtListener?.onError(cId, err?.message);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
startTLS(cId: number, tlsOptions: Object): void {
|
|
128
|
+
let socketClient: TcpSocketClient = this.socketMap.get(cId) as TcpSocketClient;
|
|
129
|
+
if (!socketClient) {
|
|
130
|
+
this.pendingTLS.set(cId, tlsOptions);
|
|
131
|
+
} else {
|
|
132
|
+
try {
|
|
133
|
+
socketClient.startTLS(tlsOptions);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
this.tcpEvtListener?.onError(cId, JSON.stringify(e));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
listen(cId: number, options: Object): void {
|
|
141
|
+
let tcpSocketServer = new TcpSocketServer(this.socketMap, this.tcpEvtListener, cId, options);
|
|
142
|
+
this.socketMap.set(cId, tcpSocketServer);
|
|
143
|
+
this.tcpEvtListener.onListen(cId, tcpSocketServer);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
close(cid: number): void {
|
|
147
|
+
let socketServer: TcpSocketServer = this.getTcpServer(cid);
|
|
148
|
+
socketServer?.close();
|
|
149
|
+
let socketClient: TcpSocketClient = this.getTcpClient(cid);
|
|
150
|
+
socketClient?.destroy();
|
|
151
|
+
this.socketMap.delete(cid);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
destroy(cid: number): void {
|
|
155
|
+
this.end(cid);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
end(cid: number): void {
|
|
159
|
+
let socketClient = this.getTcpClient(cid);
|
|
160
|
+
socketClient?.destroy();
|
|
161
|
+
this.socketMap.delete(cid);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
pause(cid: number): void {
|
|
165
|
+
let socketClient = this.getTcpClient(cid);
|
|
166
|
+
socketClient?.pause();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
resume(cid: number): void {
|
|
170
|
+
let socketClient = this.getTcpClient(cid);
|
|
171
|
+
socketClient?.resume();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
write(cId: number, base64String: string, msgId: number): void {
|
|
175
|
+
let socketClient = this.getTcpClient(cId);
|
|
176
|
+
let base64Helper = new util.Base64Helper;
|
|
177
|
+
let uint8Array = base64Helper.decodeSync(base64String)
|
|
178
|
+
let buffer = uint8Array.buffer as ArrayBuffer;
|
|
179
|
+
socketClient?.write(msgId, buffer);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
setNoDelay(cId: number, noDelay: boolean): void {
|
|
183
|
+
try {
|
|
184
|
+
let socketClient = this.getTcpClient(cId);
|
|
185
|
+
socketClient?.setNoDelay(noDelay);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
this.tcpEvtListener?.onError(cId, JSON.stringify(e));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
setKeepAlive(cId: number, enable: boolean, initialDelay: number): void {
|
|
192
|
+
try {
|
|
193
|
+
let socketClient: TcpSocketClient = this.getTcpClient(cId);
|
|
194
|
+
socketClient?.setKeepAlive(enable);
|
|
195
|
+
} catch (e) {
|
|
196
|
+
this.tcpEvtListener?.onError(cId, JSON.stringify(e));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
getPeerCertificate(cId: number): Promise<string> {
|
|
201
|
+
try {
|
|
202
|
+
let socketClient = this.getTcpClient(cId);
|
|
203
|
+
return socketClient?.getPeerCertificate();
|
|
204
|
+
} catch (e) {
|
|
205
|
+
this.tcpEvtListener?.onError(cId, JSON.stringify(e));
|
|
206
|
+
return new Promise<string>(() => {
|
|
207
|
+
return ""
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
getCertificate(cId: number): Promise<string> {
|
|
213
|
+
try {
|
|
214
|
+
let socketClient = this.getTcpClient(cId);
|
|
215
|
+
return socketClient?.getCertificate();
|
|
216
|
+
} catch (e) {
|
|
217
|
+
this.tcpEvtListener?.onError(cId, JSON.stringify(e));
|
|
218
|
+
return new Promise<string>(() => {
|
|
219
|
+
return ""
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Tag } from "@rnoh/react-native-openharmony/ts"
|
|
6
|
+
|
|
7
|
+
export namespace TcpSocketModule {
|
|
8
|
+
export const NAME = 'TcpSocketModule' as const
|
|
9
|
+
|
|
10
|
+
export interface Spec {
|
|
11
|
+
listen(cId: number, options: Object): void;
|
|
12
|
+
|
|
13
|
+
close(cid: number): void;
|
|
14
|
+
|
|
15
|
+
destroy(cid: number): void;
|
|
16
|
+
|
|
17
|
+
end(cid: number): void;
|
|
18
|
+
|
|
19
|
+
pause(cid: number): void;
|
|
20
|
+
|
|
21
|
+
resume(cid: number): void;
|
|
22
|
+
|
|
23
|
+
connect(cId: number, host: string, port: number, options: Object): void;
|
|
24
|
+
|
|
25
|
+
startTLS(cId: number, tlsOptions: Object): void;
|
|
26
|
+
|
|
27
|
+
write(cId: number, base64String: string, msgId: number): void;
|
|
28
|
+
|
|
29
|
+
setNoDelay(cId: number, noDelay: boolean): void;
|
|
30
|
+
|
|
31
|
+
setKeepAlive(cId: number, enable: boolean, initialDelay: number): void;
|
|
32
|
+
|
|
33
|
+
getPeerCertificate(cId: number): Promise<string>;
|
|
34
|
+
|
|
35
|
+
getCertificate(cId: number): Promise<string>;
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
2
|
+
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
|
3
|
+
|
|
4
|
+
export default function abilityTest() {
|
|
5
|
+
describe('ActsAbilityTest', () => {
|
|
6
|
+
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
// Presets an action, which is performed only once before all test cases of the test suite start.
|
|
9
|
+
// This API supports only one parameter: preset action function.
|
|
10
|
+
})
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
// Presets an action, which is performed before each unit test case starts.
|
|
13
|
+
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
14
|
+
// This API supports only one parameter: preset action function.
|
|
15
|
+
})
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
// Presets a clear action, which is performed after each unit test case ends.
|
|
18
|
+
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
19
|
+
// This API supports only one parameter: clear action function.
|
|
20
|
+
})
|
|
21
|
+
afterAll(() => {
|
|
22
|
+
// Presets a clear action, which is performed after all test cases of the test suite end.
|
|
23
|
+
// This API supports only one parameter: clear action function.
|
|
24
|
+
})
|
|
25
|
+
it('assertContain', 0, () => {
|
|
26
|
+
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
|
27
|
+
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
|
|
28
|
+
let a = 'abc';
|
|
29
|
+
let b = 'b';
|
|
30
|
+
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
|
31
|
+
expect(a).assertContain(b);
|
|
32
|
+
expect(a).assertEqual(a);
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
|
2
|
+
|
|
3
|
+
export default function localUnitTest() {
|
|
4
|
+
describe('localUnitTest', () => {
|
|
5
|
+
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
|
|
6
|
+
beforeAll(() => {
|
|
7
|
+
// Presets an action, which is performed only once before all test cases of the test suite start.
|
|
8
|
+
// This API supports only one parameter: preset action function.
|
|
9
|
+
});
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
// Presets an action, which is performed before each unit test case starts.
|
|
12
|
+
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
13
|
+
// This API supports only one parameter: preset action function.
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
// Presets a clear action, which is performed after each unit test case ends.
|
|
17
|
+
// The number of execution times is the same as the number of test cases defined by **it**.
|
|
18
|
+
// This API supports only one parameter: clear action function.
|
|
19
|
+
});
|
|
20
|
+
afterAll(() => {
|
|
21
|
+
// Presets a clear action, which is performed after all test cases of the test suite end.
|
|
22
|
+
// This API supports only one parameter: clear action function.
|
|
23
|
+
});
|
|
24
|
+
it('assertContain', 0, () => {
|
|
25
|
+
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
|
26
|
+
let a = 'abc';
|
|
27
|
+
let b = 'b';
|
|
28
|
+
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
|
29
|
+
expect(a).assertContain(b);
|
|
30
|
+
expect(a).assertEqual(a);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./src/main/ets/TcpSocketPackage";
|
|
7
|
+
export * from "./src/main/ets/TcpSocketTurboModule";
|
|
Binary file
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} ServerOptions
|
|
3
|
+
* @property {boolean} [noDelay]
|
|
4
|
+
* @property {boolean} [keepAlive]
|
|
5
|
+
* @property {number} [keepAliveInitialDelay]
|
|
6
|
+
* @property {boolean} [allowHalfOpen]
|
|
7
|
+
* @property {boolean} [pauseOnConnect]
|
|
8
|
+
*
|
|
9
|
+
* @typedef {import('./TLSSocket').default} TLSSocket
|
|
10
|
+
*
|
|
11
|
+
* @typedef {object} ServerEvents
|
|
12
|
+
* @property {() => void} close
|
|
13
|
+
* @property {(socket: Socket) => void} connection
|
|
14
|
+
* @property {() => void} listening
|
|
15
|
+
* @property {(err: Error) => void} error
|
|
16
|
+
* @property {(tlsSocket: TLSSocket) => void} secureConnection
|
|
17
|
+
*
|
|
18
|
+
* @extends {EventEmitter<ServerEvents, any>}
|
|
19
|
+
*/
|
|
20
|
+
export default class Server extends EventEmitter<ServerEvents, any> {
|
|
21
|
+
/**
|
|
22
|
+
* @param {ServerOptions | ((socket: Socket) => void)} [options] Server options or connection listener
|
|
23
|
+
* @param {(socket: Socket) => void} [connectionCallback] Automatically set as a listener for the `'connection'` event.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options?: ServerOptions | ((socket: Socket) => void) | undefined, connectionCallback?: ((socket: Socket) => void) | undefined);
|
|
26
|
+
/** @protected @readonly */
|
|
27
|
+
protected readonly _id: number;
|
|
28
|
+
/** @protected @readonly */
|
|
29
|
+
protected readonly _eventEmitter: import("react-native").EventEmitter;
|
|
30
|
+
/** @private @type {Set<Socket>} */
|
|
31
|
+
private _connections;
|
|
32
|
+
/** @private */
|
|
33
|
+
private _localAddress;
|
|
34
|
+
/** @private */
|
|
35
|
+
private _localPort;
|
|
36
|
+
/** @private */
|
|
37
|
+
private _localFamily;
|
|
38
|
+
/** @private @type {ServerOptions} */
|
|
39
|
+
private _serverOptions;
|
|
40
|
+
listening: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Start a server listening for connections.
|
|
43
|
+
*
|
|
44
|
+
* This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted.
|
|
45
|
+
* The last parameter `callback` will be added as a listener for the `'listening'` event.
|
|
46
|
+
*
|
|
47
|
+
* The `server.listen()` method can be called again if and only if there was an error during the first
|
|
48
|
+
* `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
|
|
49
|
+
* error will be thrown.
|
|
50
|
+
*
|
|
51
|
+
* @param {{ port: number; host?: string; reuseAddress?: boolean} | number} options Options or port
|
|
52
|
+
* @param {string | (() => void)} [callback_or_host] Callback or host string
|
|
53
|
+
* @param {() => void} [callback] Callback function
|
|
54
|
+
* @returns {Server}
|
|
55
|
+
*/
|
|
56
|
+
listen(options: {
|
|
57
|
+
port: number;
|
|
58
|
+
host?: string;
|
|
59
|
+
reuseAddress?: boolean;
|
|
60
|
+
} | number, callback_or_host?: string | (() => void) | undefined, callback?: (() => void) | undefined): Server;
|
|
61
|
+
/**
|
|
62
|
+
* Asynchronously get the number of concurrent connections on the server.
|
|
63
|
+
*
|
|
64
|
+
* Callback should take two arguments `err` and `count`.
|
|
65
|
+
*
|
|
66
|
+
* @param {(err: Error | null, count: number) => void} callback
|
|
67
|
+
* @returns {Server}
|
|
68
|
+
*/
|
|
69
|
+
getConnections(callback: (err: Error | null, count: number) => void): Server;
|
|
70
|
+
/**
|
|
71
|
+
* Stops the server from accepting new connections and keeps existing connections.
|
|
72
|
+
* This function is asynchronous, the server is finally closed when all connections are ended and the server emits a `'close'` event.
|
|
73
|
+
* The optional callback will be called once the `'close'` event occurs. Unlike that event, it will be called with an `Error` as its
|
|
74
|
+
* only argument if the server was not open when it was closed.
|
|
75
|
+
*
|
|
76
|
+
* @param {(err?: Error) => void} [callback] Called when the server is closed.
|
|
77
|
+
* @returns {Server}
|
|
78
|
+
*/
|
|
79
|
+
close(callback?: ((err?: Error | undefined) => void) | undefined): Server;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the bound `address`, the address `family` name, and `port` of the server as reported by the operating system if listening
|
|
82
|
+
* on an IP socket (useful to find which port was assigned when getting an OS-assigned address):
|
|
83
|
+
* `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
|
|
84
|
+
*
|
|
85
|
+
* @returns {import('./Socket').AddressInfo | null}
|
|
86
|
+
*/
|
|
87
|
+
address(): import('./Socket').AddressInfo | null;
|
|
88
|
+
ref(): Server;
|
|
89
|
+
unref(): Server;
|
|
90
|
+
/**
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
private _registerEvents;
|
|
94
|
+
_listeningListener: import("react-native").EmitterSubscription | undefined;
|
|
95
|
+
_errorListener: import("react-native").EmitterSubscription | undefined;
|
|
96
|
+
_connectionsListener: import("react-native").EmitterSubscription | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* @private
|
|
99
|
+
*/
|
|
100
|
+
private _setDisconnected;
|
|
101
|
+
/**
|
|
102
|
+
* @protected
|
|
103
|
+
* @param {Socket} socket
|
|
104
|
+
*/
|
|
105
|
+
protected _addConnection(socket: Socket): void;
|
|
106
|
+
/**
|
|
107
|
+
* @protected
|
|
108
|
+
* @param {{ id: number; connection: import('./Socket').NativeConnectionInfo; }} info
|
|
109
|
+
* @returns {Socket}
|
|
110
|
+
*/
|
|
111
|
+
protected _buildSocket(info: {
|
|
112
|
+
id: number;
|
|
113
|
+
connection: import('./Socket').NativeConnectionInfo;
|
|
114
|
+
}): Socket;
|
|
115
|
+
/**
|
|
116
|
+
* Apply server socket options to a newly connected socket
|
|
117
|
+
* @param {Socket} socket
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
private _applySocketOptions;
|
|
121
|
+
}
|
|
122
|
+
export type ServerOptions = {
|
|
123
|
+
noDelay?: boolean | undefined;
|
|
124
|
+
keepAlive?: boolean | undefined;
|
|
125
|
+
keepAliveInitialDelay?: number | undefined;
|
|
126
|
+
allowHalfOpen?: boolean | undefined;
|
|
127
|
+
pauseOnConnect?: boolean | undefined;
|
|
128
|
+
};
|
|
129
|
+
export type TLSSocket = import("./TLSSocket").default;
|
|
130
|
+
export type ServerEvents = {
|
|
131
|
+
close: () => void;
|
|
132
|
+
connection: (socket: Socket) => void;
|
|
133
|
+
listening: () => void;
|
|
134
|
+
error: (err: Error) => void;
|
|
135
|
+
secureConnection: (tlsSocket: TLSSocket) => void;
|
|
136
|
+
};
|
|
137
|
+
import EventEmitter from "eventemitter3";
|
|
138
|
+
import Socket from "./Socket";
|