thor-io.vnext 2.0.5-beta.3 → 3.1.0-beta.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/.gitattributes +17 -17
- package/build/index.d.ts +24 -0
- package/build/index.js +47 -0
- package/build/src/Connection/ClientInfo.d.ts +27 -0
- package/{src/Client → build/src/Connection}/ClientInfo.js +7 -0
- package/build/src/Connection/Connection.d.ts +117 -0
- package/build/src/Connection/Connection.js +202 -0
- package/build/src/Connection/IClientInfo.d.ts +20 -0
- package/build/src/Controller/ControllerBase.d.ts +134 -0
- package/build/src/Controller/ControllerBase.js +543 -0
- package/build/src/Controller/Subscription.d.ts +27 -0
- package/build/src/Controller/Subscription.js +22 -0
- package/build/src/Controllers/BrokerController/Broker.js +145 -0
- package/build/src/Controllers/BrokerController/BrokerController.d.ts +61 -0
- package/build/src/Controllers/BrokerController/BrokerController.js +115 -0
- package/{src/Controllers/BrokerController/Broker.js → build/src/Controllers/BrokerController/BrokerrController.js} +74 -85
- package/build/src/Controllers/BrokerController/Models/PeerConnection.d.ts +7 -0
- package/build/src/Controllers/BrokerController/Models/PeerConnection.js +14 -0
- package/build/src/Controllers/BrokerController/Models/Signal.js +11 -0
- package/build/src/Decorators/CanInvoke.d.ts +10 -0
- package/build/src/Decorators/CanInvoke.js +22 -0
- package/build/src/Decorators/CanSet.d.ts +10 -0
- package/build/src/Decorators/CanSet.js +20 -0
- package/build/src/Decorators/ControllerProperties.d.ts +10 -0
- package/build/src/Decorators/ControllerProperties.js +20 -0
- package/{src/Controllers/BrokerController/Models/InstantMessage.js → build/src/Interfaces/IInterceptor.js} +2 -5
- package/build/src/Interfaces/ITransport.d.ts +94 -0
- package/build/src/Interfaces/ITransportMessage.d.ts +41 -0
- package/build/src/Messages/BufferMessage.d.ts +38 -0
- package/{src → build/src}/Messages/BufferMessage.js +32 -2
- package/build/src/Messages/ErrorMessage.d.ts +29 -0
- package/build/src/Messages/ErrorMessage.js +22 -0
- package/build/src/Messages/PipeMessage.d.ts +46 -0
- package/build/src/Messages/PipeMessage.js +41 -0
- package/build/src/Messages/TextMessage.d.ts +88 -0
- package/build/src/Messages/TextMessage.js +94 -0
- package/build/src/Messages/WebSocketMessage.d.ts +34 -0
- package/build/src/Messages/WebSocketMessage.js +41 -0
- package/build/src/Plugin.js +32 -0
- package/build/src/Server/Plugin.d.ts +37 -0
- package/build/src/Server/Plugin.js +32 -0
- package/build/src/Server/ThorIOServer.d.ts +68 -0
- package/build/src/Server/ThorIOServer.js +131 -0
- package/build/src/Subscription.js +22 -0
- package/build/src/ThorIO.js +142 -0
- package/build/src/ThorIOServer.d.ts +64 -0
- package/build/src/ThorIOServer.js +131 -0
- package/build/src/Transports/BufferMessageTransport.d.ts +78 -0
- package/build/src/Transports/BufferMessageTransport.js +85 -0
- package/build/src/Transports/PipeMessageTransport.d.ts +79 -0
- package/build/src/Transports/PipeMessageTransport.js +90 -0
- package/build/src/Transports/WebSocketMessageTransport.d.ts +87 -0
- package/build/src/Transports/WebSocketMessageTransport.js +93 -0
- package/build/src/Utils/BufferUtils.d.ts +32 -0
- package/build/src/Utils/BufferUtils.js +57 -0
- package/build/src/Utils/StringUtils.d.ts +15 -0
- package/build/src/Utils/StringUtils.js +31 -0
- package/build/src/test.js +5 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +43 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1610 -0
- package/docs/classes/BrokerController.html +164 -0
- package/docs/classes/BufferMessage.html +11 -0
- package/docs/classes/BufferMessageTransport.html +31 -0
- package/docs/classes/BufferUtils.html +16 -0
- package/docs/classes/ClientInfo.html +11 -0
- package/docs/classes/Connection.html +42 -0
- package/docs/classes/ControllerBase.html +144 -0
- package/docs/classes/ErrorMessage.html +13 -0
- package/docs/classes/PeerConnection.html +4 -0
- package/docs/classes/PipeMessage.html +17 -0
- package/docs/classes/PipeMessageTransport.html +33 -0
- package/docs/classes/Plugin.html +13 -0
- package/docs/classes/Signal.html +5 -0
- package/docs/classes/StringUtils.html +9 -0
- package/docs/classes/Subscription.html +11 -0
- package/docs/classes/TextMessage.html +37 -0
- package/docs/classes/ThorIOServer.html +25 -0
- package/docs/classes/WebSocketMessage.html +18 -0
- package/docs/classes/WebSocketMessageTransport.html +35 -0
- package/docs/functions/CanInvoke.html +5 -0
- package/docs/functions/CanSet.html +5 -0
- package/docs/functions/ControllerProperties.html +5 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +24 -0
- package/docs/interfaces/ITransport.html +46 -0
- package/docs/interfaces/ITransportMessage.html +21 -0
- package/docs/modules.html +1 -0
- package/index.ts +32 -37
- package/licence +21 -21
- package/package.json +48 -50
- package/readme.md +32 -1
- package/src/{Client → Connection}/ClientInfo.ts +34 -43
- package/src/Connection/Connection.ts +229 -0
- package/src/Connection/IClientInfo.ts +22 -0
- package/src/Controller/ControllerBase.ts +561 -457
- package/src/{Subscription.ts → Controller/Subscription.ts} +32 -34
- package/src/Controllers/BrokerController/BrokerController.ts +121 -0
- package/src/Controllers/BrokerController/Models/PeerConnection.ts +12 -34
- package/src/Controllers/BrokerController/Models/Signal.ts +11 -43
- package/src/Decorators/CanInvoke.ts +21 -13
- package/src/Decorators/CanSet.ts +19 -12
- package/src/Decorators/ControllerProperties.ts +19 -16
- package/src/Interfaces/ITransport.ts +107 -23
- package/src/Interfaces/ITransportMessage.ts +45 -41
- package/src/Messages/BufferMessage.ts +70 -69
- package/src/Messages/ErrorMessage.ts +34 -6
- package/src/Messages/PipeMessage.ts +56 -62
- package/src/Messages/TextMessage.ts +139 -93
- package/src/Messages/WebSocketMessage.ts +39 -41
- package/src/Server/Plugin.ts +56 -0
- package/src/Server/ThorIOServer.ts +121 -0
- package/src/ThorIOServer.ts +117 -0
- package/src/Transports/BufferMessageTransport.ts +109 -100
- package/src/Transports/PipeMessageTransport.ts +112 -104
- package/src/Transports/WebSocketMessageTransport.ts +122 -110
- package/src/Utils/BufferUtils.ts +55 -61
- package/src/Utils/StringUtils.ts +28 -17
- package/tsconfig.json +109 -19
- package/typedoc.json +6 -0
- package/index.d.ts +0 -24
- package/index.js +0 -46
- package/src/Client/ClientInfo.d.ts +0 -6
- package/src/Connection.d.ts +0 -24
- package/src/Connection.js +0 -121
- package/src/Connection.ts +0 -263
- package/src/Controller/ControllerBase.d.ts +0 -43
- package/src/Controller/ControllerBase.js +0 -331
- package/src/Controllers/BrokerController/Broker.d.ts +0 -21
- package/src/Controllers/BrokerController/Broker.ts +0 -153
- package/src/Controllers/BrokerController/Models/InstantMessage.d.ts +0 -3
- package/src/Controllers/BrokerController/Models/InstantMessage.ts +0 -15
- package/src/Controllers/BrokerController/Models/PeerConnection.d.ts +0 -5
- package/src/Controllers/BrokerController/Models/PeerConnection.js +0 -9
- package/src/Controllers/BrokerController/Models/Signal.js +0 -10
- package/src/Decorators/CanInvoke.d.ts +0 -2
- package/src/Decorators/CanInvoke.js +0 -9
- package/src/Decorators/CanSet.d.ts +0 -1
- package/src/Decorators/CanSet.js +0 -8
- package/src/Decorators/ControllerProperties.d.ts +0 -1
- package/src/Decorators/ControllerProperties.js +0 -10
- package/src/Interfaces/IInterceptor.d.ts +0 -2
- package/src/Interfaces/IInterceptor.ts +0 -2
- package/src/Interfaces/ITransport.d.ts +0 -18
- package/src/Interfaces/ITransportMessage.d.ts +0 -8
- package/src/Listener.d.ts +0 -5
- package/src/Listener.js +0 -9
- package/src/Listener.ts +0 -34
- package/src/Messages/BufferMessage.d.ts +0 -10
- package/src/Messages/ErrorMessage.d.ts +0 -4
- package/src/Messages/ErrorMessage.js +0 -8
- package/src/Messages/PipeMessage.d.ts +0 -12
- package/src/Messages/PipeMessage.js +0 -20
- package/src/Messages/TextMessage.d.ts +0 -15
- package/src/Messages/TextMessage.js +0 -50
- package/src/Messages/WebSocketMessage.d.ts +0 -10
- package/src/Messages/WebSocketMessage.js +0 -15
- package/src/Plugin.d.ts +0 -6
- package/src/Plugin.js +0 -11
- package/src/Plugin.ts +0 -37
- package/src/Subscription.d.ts +0 -5
- package/src/Subscription.js +0 -9
- package/src/ThorIO.d.ts +0 -18
- package/src/ThorIO.js +0 -65
- package/src/ThorIO.ts +0 -148
- package/src/Transports/BufferMessageTransport.d.ts +0 -21
- package/src/Transports/BufferMessageTransport.js +0 -31
- package/src/Transports/PipeMessageTransport.d.ts +0 -20
- package/src/Transports/PipeMessageTransport.js +0 -33
- package/src/Transports/WebSocketMessageTransport.d.ts +0 -20
- package/src/Transports/WebSocketMessageTransport.js +0 -31
- package/src/Utils/BufferUtils.d.ts +0 -5
- package/src/Utils/BufferUtils.js +0 -29
- package/src/Utils/StringUtils.d.ts +0 -4
- package/src/Utils/StringUtils.js +0 -13
- /package/{src/Interfaces/IInterceptor.js → build/src/Connection/IClientInfo.js} +0 -0
- /package/{src → build/src}/Controllers/BrokerController/Models/Signal.d.ts +0 -0
- /package/{src → build/src}/Interfaces/ITransport.js +0 -0
- /package/{src → build/src}/Interfaces/ITransportMessage.js +0 -0
package/package.json
CHANGED
|
@@ -1,50 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "thor-io.vnext",
|
|
3
|
-
"author": {
|
|
4
|
-
"name": "Magnus Thor",
|
|
5
|
-
"email": "magnus.thor74@gmail.com"
|
|
6
|
-
},
|
|
7
|
-
"contributors": [
|
|
8
|
-
{
|
|
9
|
-
"name": "Ricky Davies",
|
|
10
|
-
"email": "rdavies@zen.co.uk"
|
|
11
|
-
}
|
|
12
|
-
],
|
|
13
|
-
"description": "thor-io.vnext represents the next generation of thor-io.Thor-IO is a fundament for stateful real-time communication.TCP/IP,WebSockets & WebRTC or whatever you need and desire.",
|
|
14
|
-
"version": "
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"main": "index.js",
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"RPC",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"@types/
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
"express": "^4.
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "thor-io.vnext",
|
|
3
|
+
"author": {
|
|
4
|
+
"name": "Magnus Thor",
|
|
5
|
+
"email": "magnus.thor74@gmail.com"
|
|
6
|
+
},
|
|
7
|
+
"contributors": [
|
|
8
|
+
{
|
|
9
|
+
"name": "Ricky Davies",
|
|
10
|
+
"email": "rdavies@zen.co.uk"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
"description": "thor-io.vnext represents the next generation of thor-io.Thor-IO is a fundament for stateful real-time communication.TCP/IP,WebSockets & WebRTC or whatever you need and desire.",
|
|
14
|
+
"version": "3.1.0-beta.1",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"main": "build/index.js",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/MagnusThor/thor-io.vnext.git"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"unity",
|
|
23
|
+
".net",
|
|
24
|
+
"realtime",
|
|
25
|
+
"websockets",
|
|
26
|
+
"WebRTC",
|
|
27
|
+
"PubSub",
|
|
28
|
+
"RPC",
|
|
29
|
+
"Smart-RPC",
|
|
30
|
+
"IoT",
|
|
31
|
+
"NodeJS",
|
|
32
|
+
"RTC",
|
|
33
|
+
"Typescript"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@types/node": "^10.17.26",
|
|
37
|
+
"@types/reflect-metadata": "^0.1.0",
|
|
38
|
+
"ws": "^7.3.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/ws": "^7.2.5",
|
|
42
|
+
"express": "^4.17.1",
|
|
43
|
+
"express-ws": "^4.0.0",
|
|
44
|
+
"typedoc": "^0.27.6",
|
|
45
|
+
"typings": "^0.6.10",
|
|
46
|
+
"webpack": "^5.97.1"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/readme.md
CHANGED
|
@@ -1 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
**The Foundation for Next-Gen, Stateful Real-Time Communication**
|
|
2
|
+
|
|
3
|
+
thor-io.vnext empowers you to build cutting-edge, real-time applications with unparalleled flexibility and performance.
|
|
4
|
+
|
|
5
|
+
**Key Features:**
|
|
6
|
+
|
|
7
|
+
* **Rock-Solid Foundation:** Built for demanding applications, thor-io.vnext provides a robust framework for stateful real-time communication.
|
|
8
|
+
* **Unleash Your Creativity:** Seamlessly integrate with your preferred transport layer: TCP/IP, WebSockets, WebRTC, or any custom solution you envision.
|
|
9
|
+
* **Effortless Integration:** Easily plug into your existing Node.js ecosystem – works flawlessly with Express and other popular middleware.
|
|
10
|
+
* **WebRTC Signaling Made Easy:** Leverage built-in WebRTC signaling capabilities to simplify the development of peer-to-peer applications.
|
|
11
|
+
|
|
12
|
+
**What can you build with thor-io.vnext?**
|
|
13
|
+
|
|
14
|
+
* **Real-time Collaboration:** Power collaborative tools like shared editors, whiteboards, and online design platforms.
|
|
15
|
+
* **Immersive Experiences:** Create captivating online games, interactive simulations, and virtual/augmented reality applications.
|
|
16
|
+
* **High-Performance Applications:** Build robust applications that demand real-time data synchronization, such as financial trading platforms and IoT solutions.
|
|
17
|
+
* **And Much More!** The possibilities are limited only by your imagination.
|
|
18
|
+
|
|
19
|
+
**Getting Started:**
|
|
20
|
+
|
|
21
|
+
**Install**
|
|
22
|
+
```bash
|
|
23
|
+
npm install thor-io.vnext
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Documentation**
|
|
27
|
+
|
|
28
|
+
https://../doc
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
/Magnus Thor
|
|
@@ -1,43 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @type {string}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @type {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
* @memberOf ClientInfo
|
|
37
|
-
*/
|
|
38
|
-
constructor(ci: string, controller: string) {
|
|
39
|
-
this.CI = ci;
|
|
40
|
-
this.C = controller;
|
|
41
|
-
this.TS = new Date();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
1
|
+
export class ClientInfo {
|
|
2
|
+
/**
|
|
3
|
+
* The client identifier.
|
|
4
|
+
*
|
|
5
|
+
* @type {string}
|
|
6
|
+
*/
|
|
7
|
+
public CI: string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The controller identifier.
|
|
11
|
+
*
|
|
12
|
+
* @type {string}
|
|
13
|
+
*/
|
|
14
|
+
public C: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The timestamp when the instance was created.
|
|
18
|
+
*
|
|
19
|
+
* @type {Date}
|
|
20
|
+
*/
|
|
21
|
+
public TS: Date;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of ClientInfo.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} ci - The client identifier.
|
|
27
|
+
* @param {string} controller - The controller identifier.
|
|
28
|
+
*/
|
|
29
|
+
constructor(ci: string, controller: string) {
|
|
30
|
+
this.CI = ci;
|
|
31
|
+
this.C = controller;
|
|
32
|
+
this.TS = new Date();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { ControllerBase } from '../Controller/ControllerBase';
|
|
2
|
+
import { ITransport } from '../Interfaces/ITransport';
|
|
3
|
+
import { ITransportMessage } from '../Interfaces/ITransportMessage';
|
|
4
|
+
import { TextMessage } from '../Messages/TextMessage';
|
|
5
|
+
import { Plugin } from '../Server/Plugin';
|
|
6
|
+
import { ClientInfo } from './ClientInfo';
|
|
7
|
+
|
|
8
|
+
export class Connection {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An array to store errors.
|
|
12
|
+
* @public
|
|
13
|
+
* @type {Array<any>}
|
|
14
|
+
*/
|
|
15
|
+
public errors: Array<any> = [];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The ping pong interval.
|
|
19
|
+
* @public
|
|
20
|
+
* @type {number}
|
|
21
|
+
*/
|
|
22
|
+
public pingPongInterval: number = 60;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A map to store the controller instances.
|
|
26
|
+
* @public
|
|
27
|
+
* @type {Map<string, ControllerBase>}
|
|
28
|
+
*/
|
|
29
|
+
public controllerInstances: Map<string, ControllerBase> = new Map();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Client information.
|
|
33
|
+
* @public
|
|
34
|
+
* @type {ClientInfo | undefined}
|
|
35
|
+
*/
|
|
36
|
+
public clientInfo: ClientInfo | undefined;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Tries to invoke a method or set/get a property on the controller.
|
|
40
|
+
* @private
|
|
41
|
+
* @param {ControllerBase} controller The controller instance.
|
|
42
|
+
* @param {string} methodOrProperty The method or property name.
|
|
43
|
+
* @param {string} data The data to be passed to the method or property.
|
|
44
|
+
* @param {Buffer} [buffer] An optional buffer.
|
|
45
|
+
*/
|
|
46
|
+
private tryInvokeMethod(
|
|
47
|
+
controller: ControllerBase,
|
|
48
|
+
methodOrProperty: string,
|
|
49
|
+
data: string,
|
|
50
|
+
buffer?: Buffer
|
|
51
|
+
) {
|
|
52
|
+
if (controller.canInvokeMethod(methodOrProperty)) {
|
|
53
|
+
controller.invokeMethod(methodOrProperty, data, buffer);
|
|
54
|
+
} else if (controller.canSetProperty(methodOrProperty)) {
|
|
55
|
+
controller.setProperty(methodOrProperty, JSON.parse(data));
|
|
56
|
+
} else if (controller.canGetProperty(methodOrProperty)) {
|
|
57
|
+
const { resultId } = JSON.parse(data) as { resultId: string };
|
|
58
|
+
controller.getProperty(methodOrProperty, resultId);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Getter for the connection ID.
|
|
64
|
+
* @public
|
|
65
|
+
* @returns {string} The connection ID.
|
|
66
|
+
*/
|
|
67
|
+
get id(): string {
|
|
68
|
+
return this.transport.id;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Constructor for the Connection class.
|
|
73
|
+
* @param {ITransport} transport The transport instance.
|
|
74
|
+
* @param {Map<string, Connection>} connections A map of connections.
|
|
75
|
+
* @param {Map<string, Plugin<ControllerBase>>} controllers A map of controllers.
|
|
76
|
+
* @constructor
|
|
77
|
+
*/
|
|
78
|
+
constructor(
|
|
79
|
+
public transport: ITransport,
|
|
80
|
+
public connections: Map<string, Connection>,
|
|
81
|
+
private controllers: Map<string, Plugin<ControllerBase>>
|
|
82
|
+
) {
|
|
83
|
+
try {
|
|
84
|
+
this.setupTransport(transport);
|
|
85
|
+
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.log('Error in Connection constructor', error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sets up the transport event listeners.
|
|
93
|
+
* @private
|
|
94
|
+
* @param {ITransport} transport The transport instance.
|
|
95
|
+
*/
|
|
96
|
+
private setupTransport(transport: ITransport) {
|
|
97
|
+
if (transport) {
|
|
98
|
+
this.transport.onMessage = (event: ITransportMessage) => {
|
|
99
|
+
try {
|
|
100
|
+
const message = event.isBinary ? TextMessage.fromArrayBuffer(event.data) : event.toMessage();
|
|
101
|
+
const controller = this.tryCreateControllerInstance(message.C);
|
|
102
|
+
if (controller) {
|
|
103
|
+
this.tryInvokeMethod(controller, message.T, message.D, message.B);
|
|
104
|
+
}
|
|
105
|
+
} catch (error) {
|
|
106
|
+
this.addError(error);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Adds an error to the errors array.
|
|
114
|
+
* @private
|
|
115
|
+
* @param {any} error The error to be added.
|
|
116
|
+
*/
|
|
117
|
+
private addError(error: any) {
|
|
118
|
+
this.errors.push(error);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Checks if a controller with the given alias exists.
|
|
123
|
+
* @public
|
|
124
|
+
* @param {string} alias The controller alias.
|
|
125
|
+
* @returns {boolean} True if the controller exists, false otherwise.
|
|
126
|
+
*/
|
|
127
|
+
hasController(alias: string): boolean {
|
|
128
|
+
return this.controllerInstances.has(alias);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Removes a controller from the controllerInstances.
|
|
133
|
+
* @public
|
|
134
|
+
* @param {string} alias The alias of the controller to be removed.
|
|
135
|
+
* @returns {boolean} True if the controller was removed successfully, false otherwise.
|
|
136
|
+
*/
|
|
137
|
+
tryRemoveControllerInstance(alias: string): boolean {
|
|
138
|
+
return this.controllerInstances.delete(alias);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Gets a controller instance from the controllerInstances map.
|
|
143
|
+
* @public
|
|
144
|
+
* @param {string} alias The alias of the controller to be retrieved.
|
|
145
|
+
* @returns {ControllerBase | undefined} The controller instance if found, otherwise undefined.
|
|
146
|
+
*/
|
|
147
|
+
tryGetController(alias: string): ControllerBase | undefined {
|
|
148
|
+
try {
|
|
149
|
+
const match = this.controllerInstances.get(alias);
|
|
150
|
+
if (!match) throw new Error(`Cannot locate the requested controller ${alias}`);
|
|
151
|
+
return match;
|
|
152
|
+
} catch (error) {
|
|
153
|
+
this.addError(error);
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Adds a controller instance to the controllerInstances map.
|
|
160
|
+
* @private
|
|
161
|
+
* @param {ControllerBase} controller The controller instance to be added.
|
|
162
|
+
* @returns {ControllerBase} The added controller instance.
|
|
163
|
+
*/
|
|
164
|
+
private addControllerInstance(controller: ControllerBase): ControllerBase {
|
|
165
|
+
if (!controller.alias) throw `Cannot add Controller instance`;
|
|
166
|
+
this.controllerInstances.set(controller.alias, controller);
|
|
167
|
+
return controller;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Finds and resolves a controller by alias.
|
|
172
|
+
* @public
|
|
173
|
+
* @param {string} alias The alias of the controller to be resolved.
|
|
174
|
+
* @returns {ControllerBase} The resolved controller instance.
|
|
175
|
+
* @throws {Error} If the controller cannot be resolved.
|
|
176
|
+
*/
|
|
177
|
+
public tryResolveController(alias: string): ControllerBase {
|
|
178
|
+
const plugin = this.controllers.get(alias);
|
|
179
|
+
if (!plugin) {
|
|
180
|
+
throw new Error(`Cannot resolve ${alias}, controller unknown.`);
|
|
181
|
+
}
|
|
182
|
+
return plugin.getInstance();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Locates a controller by alias. If registered, returns it; otherwise, creates an instance.
|
|
187
|
+
* @public
|
|
188
|
+
* @param {string} alias The alias of the controller to be located.
|
|
189
|
+
* @returns {ControllerBase | undefined} The located controller instance, or undefined if not found.
|
|
190
|
+
*/
|
|
191
|
+
tryCreateControllerInstance(alias: string): ControllerBase | undefined {
|
|
192
|
+
try {
|
|
193
|
+
let instancedController = this.tryGetController(alias);
|
|
194
|
+
if (instancedController) {
|
|
195
|
+
return instancedController;
|
|
196
|
+
} else {
|
|
197
|
+
const resolvedController = this.tryResolveController(alias);
|
|
198
|
+
const controllerInstance = new resolvedController(this);
|
|
199
|
+
this.addControllerInstance(controllerInstance);
|
|
200
|
+
this.initializeControllerInstance(controllerInstance);
|
|
201
|
+
return controllerInstance;
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
|
|
205
|
+
this.transport.close(
|
|
206
|
+
1011,
|
|
207
|
+
`Cannot locate the specified controller, it may be sealed or the alias is unknown '${alias}'. Connection closed.`
|
|
208
|
+
);
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Initializes a newly created controller instance.
|
|
215
|
+
* @private
|
|
216
|
+
* @param {ControllerBase} controllerInstance The controller instance to initialize.
|
|
217
|
+
*/
|
|
218
|
+
private initializeControllerInstance(controllerInstance: ControllerBase) {
|
|
219
|
+
controllerInstance.invoke(
|
|
220
|
+
new ClientInfo(this.id, controllerInstance.alias!),
|
|
221
|
+
'___open',
|
|
222
|
+
controllerInstance.alias!
|
|
223
|
+
);
|
|
224
|
+
if (controllerInstance.onopen) controllerInstance.onopen();
|
|
225
|
+
this.transport.onClose = (e: any) => {
|
|
226
|
+
if (controllerInstance.onclose) controllerInstance.onclose();
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface IClientInfo {
|
|
2
|
+
/**
|
|
3
|
+
* The client identifier.
|
|
4
|
+
*
|
|
5
|
+
* @type {string}
|
|
6
|
+
*/
|
|
7
|
+
CI: string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The controller identifier.
|
|
11
|
+
*
|
|
12
|
+
* @type {string}
|
|
13
|
+
*/
|
|
14
|
+
C: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The timestamp when the instance was created.
|
|
18
|
+
*
|
|
19
|
+
* @type {Date}
|
|
20
|
+
*/
|
|
21
|
+
TS: Date;
|
|
22
|
+
}
|