thor-io.vnext 3.1.0-beta.1 → 3.2.0-beta.4
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.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as net from 'net';
|
|
2
|
+
import { Connection } from '../Connection/Connection';
|
|
2
3
|
import { ControllerBase } from '../Controller/ControllerBase';
|
|
3
4
|
import { ITransport } from '../Interfaces/ITransport';
|
|
5
|
+
import { Plugin } from '../Server/Plugin';
|
|
4
6
|
/**
|
|
5
7
|
* ThorIOServer server class for managing connections and controllers.
|
|
6
8
|
*/
|
|
@@ -8,11 +10,11 @@ export declare class ThorIOServer {
|
|
|
8
10
|
/**
|
|
9
11
|
* An array of plugins containing registered controllers.
|
|
10
12
|
*/
|
|
11
|
-
|
|
13
|
+
controllers: Map<string, Plugin<ControllerBase>>;
|
|
12
14
|
/**
|
|
13
15
|
* A Map of connections indexed by their ID.
|
|
14
16
|
*/
|
|
15
|
-
|
|
17
|
+
connections: Map<string, Connection>;
|
|
16
18
|
/**
|
|
17
19
|
* An array of active net.Server instances representing endpoints.
|
|
18
20
|
*/
|
|
@@ -65,4 +67,40 @@ export declare class ThorIOServer {
|
|
|
65
67
|
* @param {ITransport} transport The transport object representing the connection.
|
|
66
68
|
*/
|
|
67
69
|
private addConnection;
|
|
70
|
+
/**
|
|
71
|
+
* Publishes a message to all connected clients that are subscribed to the specified topic.
|
|
72
|
+
* @template T - The type of data being published.
|
|
73
|
+
* @param data - The data to publish.
|
|
74
|
+
* @param topic - The topic to publish to.
|
|
75
|
+
* @param controller - The controller handling the message.
|
|
76
|
+
*/
|
|
77
|
+
publishToAll<T>(data: T, topic: string, controller: string): void;
|
|
78
|
+
publishTo<T>(connectionId: string, data: T, topic: string, controller: string): void;
|
|
79
|
+
/**
|
|
80
|
+
* Invokes a method on all connected clients for a specific controller and topic.
|
|
81
|
+
*
|
|
82
|
+
* @param data - The data to be sent to each controller's invoke method.
|
|
83
|
+
* @param topic - The topic or event name to be invoked.
|
|
84
|
+
* @param controller - The name of the controller to target on each connection.
|
|
85
|
+
* @param buffer - Optional binary data to be passed along with the invocation.
|
|
86
|
+
*/
|
|
87
|
+
invokeToAll<T>(data: T, topic: string, controller: string, buffer?: any): void;
|
|
88
|
+
/**
|
|
89
|
+
* Invokes a method on a connected client.
|
|
90
|
+
* @param connectionId - The unique identifier of the target client connection.
|
|
91
|
+
* @param data - The data payload to send to the client.
|
|
92
|
+
* @param topic - The topic or method name to invoke on the client.
|
|
93
|
+
* @param controller - The name of the controller handling the invocation.
|
|
94
|
+
* @param buffer - Optional buffer data to include with the invocation.
|
|
95
|
+
*/
|
|
96
|
+
invokeOnClient<T>(connectionId: string, data: T, topic: string, controller: string, buffer?: any): void;
|
|
97
|
+
/**
|
|
98
|
+
* Finds and returns an array of controllers of the specified type that match the given predicate.
|
|
99
|
+
*
|
|
100
|
+
* @template T - The type of the controller to find.
|
|
101
|
+
* @param controller - The name of the controller to search for.
|
|
102
|
+
* @param predicate - A function that takes a controller instance and returns a boolean indicating whether it matches the criteria.
|
|
103
|
+
* @returns An array of matching `ControllerBase` instances if any are found; otherwise, `undefined`.
|
|
104
|
+
*/
|
|
105
|
+
findConnecion<T>(controller: string, predicate: (item: any) => boolean): ControllerBase[] | undefined;
|
|
68
106
|
}
|
|
@@ -38,6 +38,7 @@ const net = __importStar(require("net"));
|
|
|
38
38
|
const Connection_1 = require("../Connection/Connection");
|
|
39
39
|
const Plugin_1 = require("../Server/Plugin");
|
|
40
40
|
const WebSocketMessageTransport_1 = require("../Transports/WebSocketMessageTransport");
|
|
41
|
+
const TextMessage_1 = require("../Messages/TextMessage");
|
|
41
42
|
/**
|
|
42
43
|
* ThorIOServer server class for managing connections and controllers.
|
|
43
44
|
*/
|
|
@@ -127,5 +128,80 @@ class ThorIOServer {
|
|
|
127
128
|
});
|
|
128
129
|
this.connections.set(transport.id, new Connection_1.Connection(transport, this.connections, this.controllers));
|
|
129
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Publishes a message to all connected clients that are subscribed to the specified topic.
|
|
133
|
+
* @template T - The type of data being published.
|
|
134
|
+
* @param data - The data to publish.
|
|
135
|
+
* @param topic - The topic to publish to.
|
|
136
|
+
* @param controller - The controller handling the message.
|
|
137
|
+
*/
|
|
138
|
+
publishToAll(data, topic, controller) {
|
|
139
|
+
let msg = new TextMessage_1.TextMessage(topic, data, controller);
|
|
140
|
+
this.connections.forEach(connection => {
|
|
141
|
+
const ctrl = connection.tryGetController(controller);
|
|
142
|
+
if (ctrl) {
|
|
143
|
+
if (ctrl.getSubscription(topic)) {
|
|
144
|
+
connection.transport.send(msg.toString());
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
publishTo(connectionId, data, topic, controller) {
|
|
150
|
+
const connection = this.connections.get(connectionId);
|
|
151
|
+
if (connection) {
|
|
152
|
+
const ctrl = connection.tryGetController(controller);
|
|
153
|
+
if (ctrl) {
|
|
154
|
+
ctrl.publish(data, topic, controller);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Invokes a method on all connected clients for a specific controller and topic.
|
|
160
|
+
*
|
|
161
|
+
* @param data - The data to be sent to each controller's invoke method.
|
|
162
|
+
* @param topic - The topic or event name to be invoked.
|
|
163
|
+
* @param controller - The name of the controller to target on each connection.
|
|
164
|
+
* @param buffer - Optional binary data to be passed along with the invocation.
|
|
165
|
+
*/
|
|
166
|
+
invokeToAll(data, topic, controller, buffer) {
|
|
167
|
+
this.connections.forEach((connection) => {
|
|
168
|
+
const ctrl = connection.tryGetController(controller);
|
|
169
|
+
if (ctrl) {
|
|
170
|
+
ctrl.invoke(data, topic, controller, buffer);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Invokes a method on a connected client.
|
|
176
|
+
* @param connectionId - The unique identifier of the target client connection.
|
|
177
|
+
* @param data - The data payload to send to the client.
|
|
178
|
+
* @param topic - The topic or method name to invoke on the client.
|
|
179
|
+
* @param controller - The name of the controller handling the invocation.
|
|
180
|
+
* @param buffer - Optional buffer data to include with the invocation.
|
|
181
|
+
*/
|
|
182
|
+
invokeOnClient(connectionId, data, topic, controller, buffer) {
|
|
183
|
+
const connection = this.connections.get(connectionId);
|
|
184
|
+
if (connection) {
|
|
185
|
+
let targetController = connection.tryGetController(controller);
|
|
186
|
+
if (targetController) {
|
|
187
|
+
targetController.invoke(data, topic, controller, buffer);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Finds and returns an array of controllers of the specified type that match the given predicate.
|
|
193
|
+
*
|
|
194
|
+
* @template T - The type of the controller to find.
|
|
195
|
+
* @param controller - The name of the controller to search for.
|
|
196
|
+
* @param predicate - A function that takes a controller instance and returns a boolean indicating whether it matches the criteria.
|
|
197
|
+
* @returns An array of matching `ControllerBase` instances if any are found; otherwise, `undefined`.
|
|
198
|
+
*/
|
|
199
|
+
findConnecion(controller, predicate) {
|
|
200
|
+
let connections = Array.from(this.connections.values()).map((p) => {
|
|
201
|
+
return p.tryGetController(controller);
|
|
202
|
+
});
|
|
203
|
+
const result = connections.filter(predicate).filter((c) => c !== undefined);
|
|
204
|
+
return result.length > 0 ? result : undefined;
|
|
205
|
+
}
|
|
130
206
|
}
|
|
131
207
|
exports.ThorIOServer = ThorIOServer;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
}
|
|
12
12
|
],
|
|
13
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.
|
|
14
|
+
"version": "3.2.0-beta.4",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"main": "build/index.js",
|
|
17
17
|
"repository": {
|
|
@@ -5,8 +5,10 @@ import { ControllerBase } from '../Controller/ControllerBase';
|
|
|
5
5
|
import { ITransport } from '../Interfaces/ITransport';
|
|
6
6
|
import { Plugin } from '../Server/Plugin';
|
|
7
7
|
import {
|
|
8
|
-
|
|
8
|
+
WebSocketMessageTransport,
|
|
9
9
|
} from '../Transports/WebSocketMessageTransport';
|
|
10
|
+
import { WebSocketMessage } from '../Messages/WebSocketMessage';
|
|
11
|
+
import { TextMessage } from '../Messages/TextMessage';
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* ThorIOServer server class for managing connections and controllers.
|
|
@@ -15,11 +17,11 @@ export class ThorIOServer {
|
|
|
15
17
|
/**
|
|
16
18
|
* An array of plugins containing registered controllers.
|
|
17
19
|
*/
|
|
18
|
-
|
|
20
|
+
public controllers: Map<string, Plugin<ControllerBase>>;
|
|
19
21
|
/**
|
|
20
22
|
* A Map of connections indexed by their ID.
|
|
21
23
|
*/
|
|
22
|
-
|
|
24
|
+
public connections: Map<string, Connection>;
|
|
23
25
|
/**
|
|
24
26
|
* An array of active net.Server instances representing endpoints.
|
|
25
27
|
*/
|
|
@@ -118,4 +120,89 @@ export class ThorIOServer {
|
|
|
118
120
|
});
|
|
119
121
|
this.connections.set(transport.id, new Connection(transport, this.connections, this.controllers));
|
|
120
122
|
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Publishes a message to all connected clients that are subscribed to the specified topic.
|
|
126
|
+
* @template T - The type of data being published.
|
|
127
|
+
* @param data - The data to publish.
|
|
128
|
+
* @param topic - The topic to publish to.
|
|
129
|
+
* @param controller - The controller handling the message.
|
|
130
|
+
*/
|
|
131
|
+
publishToAll<T>(data: T, topic: string, controller: string) {
|
|
132
|
+
let msg = new TextMessage(topic, data, controller);
|
|
133
|
+
this.connections.forEach(connection => {
|
|
134
|
+
const ctrl = connection.tryGetController(controller);
|
|
135
|
+
if (ctrl) {
|
|
136
|
+
if (ctrl.getSubscription(topic)) {
|
|
137
|
+
connection.transport.send(msg.toString());
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
publishTo<T>(connectionId: string, data: T, topic: string, controller: string) {
|
|
145
|
+
const connection = this.connections.get(connectionId);
|
|
146
|
+
if (connection!) {
|
|
147
|
+
const ctrl = connection.tryGetController(controller);
|
|
148
|
+
if (ctrl) {
|
|
149
|
+
ctrl.publish(data, topic, controller);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Invokes a method on all connected clients for a specific controller and topic.
|
|
157
|
+
*
|
|
158
|
+
* @param data - The data to be sent to each controller's invoke method.
|
|
159
|
+
* @param topic - The topic or event name to be invoked.
|
|
160
|
+
* @param controller - The name of the controller to target on each connection.
|
|
161
|
+
* @param buffer - Optional binary data to be passed along with the invocation.
|
|
162
|
+
*/
|
|
163
|
+
invokeToAll<T>(data: T, topic: string, controller: string, buffer?: any) {
|
|
164
|
+
this.connections.forEach((connection: Connection) => {
|
|
165
|
+
const ctrl = connection.tryGetController(controller);
|
|
166
|
+
if (ctrl) {
|
|
167
|
+
ctrl.invoke(data, topic, controller, buffer);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Invokes a method on a connected client.
|
|
173
|
+
* @param connectionId - The unique identifier of the target client connection.
|
|
174
|
+
* @param data - The data payload to send to the client.
|
|
175
|
+
* @param topic - The topic or method name to invoke on the client.
|
|
176
|
+
* @param controller - The name of the controller handling the invocation.
|
|
177
|
+
* @param buffer - Optional buffer data to include with the invocation.
|
|
178
|
+
*/
|
|
179
|
+
invokeOnClient<T>(connectionId: string, data: T, topic: string, controller: string, buffer?: any) {
|
|
180
|
+
const connection = this.connections.get(connectionId);
|
|
181
|
+
if (connection!) {
|
|
182
|
+
let targetController = connection.tryGetController(controller);
|
|
183
|
+
if (targetController) {
|
|
184
|
+
targetController.invoke(data, topic, controller, buffer);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Finds and returns an array of controllers of the specified type that match the given predicate.
|
|
193
|
+
*
|
|
194
|
+
* @template T - The type of the controller to find.
|
|
195
|
+
* @param controller - The name of the controller to search for.
|
|
196
|
+
* @param predicate - A function that takes a controller instance and returns a boolean indicating whether it matches the criteria.
|
|
197
|
+
* @returns An array of matching `ControllerBase` instances if any are found; otherwise, `undefined`.
|
|
198
|
+
*/
|
|
199
|
+
findConnecion<T>(controller: string, predicate: (item: any) => boolean): ControllerBase[] | undefined {
|
|
200
|
+
let connections = Array.from(this.connections.values()).map((p: Connection) => {
|
|
201
|
+
return p.tryGetController(controller);
|
|
202
|
+
});
|
|
203
|
+
const result = connections.filter(predicate).filter((c): c is ControllerBase => c !== undefined);
|
|
204
|
+
return result.length > 0 ? result : undefined;
|
|
205
|
+
}
|
|
206
|
+
|
|
121
207
|
}
|
|
208
|
+
|