@tsed/socketio-testing 6.100.3 → 6.102.2
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/package.json +7 -7
- package/readme.md +82 -96
- package/tsconfig.compile.esm.json +1 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/socketio-testing",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.102.2",
|
|
4
4
|
"description": "Socket.io testing package for Ts.ED framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -22,15 +22,15 @@
|
|
|
22
22
|
"tslib": "2.2.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@tsed/common": "6.
|
|
26
|
-
"@tsed/di": "6.
|
|
25
|
+
"@tsed/common": "6.102.2",
|
|
26
|
+
"@tsed/di": "6.102.2",
|
|
27
27
|
"socket.io-client": "^4.1.2"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@tsed/common": "^6.
|
|
31
|
-
"@tsed/core": "^6.
|
|
32
|
-
"@tsed/di": "^6.
|
|
33
|
-
"@tsed/socketio": "^6.
|
|
30
|
+
"@tsed/common": "^6.102.2",
|
|
31
|
+
"@tsed/core": "^6.102.2",
|
|
32
|
+
"@tsed/di": "^6.102.2",
|
|
33
|
+
"@tsed/socketio": "^6.102.2",
|
|
34
34
|
"socket.io-client": "^4.0.1"
|
|
35
35
|
},
|
|
36
36
|
"repository": "https://github.com/tsedio/tsed",
|
package/readme.md
CHANGED
|
@@ -44,24 +44,22 @@ Then add the following configuration in your Server:
|
|
|
44
44
|
import {Configuration} from "@tsed/common";
|
|
45
45
|
import "@tsed/socketio"; // import socketio Ts.ED module
|
|
46
46
|
import {resolve} from "path";
|
|
47
|
-
const rootDir = resolve(__dirname)
|
|
47
|
+
const rootDir = resolve(__dirname);
|
|
48
48
|
|
|
49
49
|
@Configuration({
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
rootDir,
|
|
51
|
+
socketIO: {
|
|
52
|
+
// ... see configuration
|
|
53
|
+
}
|
|
54
54
|
})
|
|
55
|
-
export class Server {
|
|
56
|
-
|
|
57
|
-
}
|
|
55
|
+
export class Server {}
|
|
58
56
|
```
|
|
59
57
|
|
|
60
58
|
## Socket Service
|
|
61
59
|
|
|
62
60
|
> Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths.
|
|
63
|
-
This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application
|
|
64
|
-
by introducing separation between communication channels. See [namespace documentation](https://socket.io/docs/rooms-and-namespaces/#).
|
|
61
|
+
> This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application
|
|
62
|
+
> by introducing separation between communication channels. See [namespace documentation](https://socket.io/docs/rooms-and-namespaces/#).
|
|
65
63
|
|
|
66
64
|
All Socket service work under a namespace and you can create one Socket service per namespace.
|
|
67
65
|
|
|
@@ -73,31 +71,24 @@ import {SocketService, IO, Nsp, Socket, SocketSession} from "@tsed/socketio";
|
|
|
73
71
|
|
|
74
72
|
@SocketService("/my-namespace")
|
|
75
73
|
export class MySocketService {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Triggered when a client disconnects from the Namespace.
|
|
97
|
-
*/
|
|
98
|
-
$onDisconnect(@Socket socket: SocketIO.Socket) {
|
|
99
|
-
|
|
100
|
-
}
|
|
74
|
+
@Nsp nsp: SocketIO.Namespace;
|
|
75
|
+
|
|
76
|
+
@Nsp("/my-other-namespace")
|
|
77
|
+
nspOther: SocketIO.Namespace; // communication between two namespace
|
|
78
|
+
|
|
79
|
+
constructor(@IO private io: SocketIO.Server) {}
|
|
80
|
+
/**
|
|
81
|
+
* Triggered the namespace is created
|
|
82
|
+
*/
|
|
83
|
+
$onNamespaceInit(nsp: SocketIO.Namespace) {}
|
|
84
|
+
/**
|
|
85
|
+
* Triggered when a new client connects to the Namespace.
|
|
86
|
+
*/
|
|
87
|
+
$onConnection(@Socket socket: SocketIO.Socket, @SocketSession session: SocketSession) {}
|
|
88
|
+
/**
|
|
89
|
+
* Triggered when a client disconnects from the Namespace.
|
|
90
|
+
*/
|
|
91
|
+
$onDisconnect(@Socket socket: SocketIO.Socket) {}
|
|
101
92
|
}
|
|
102
93
|
```
|
|
103
94
|
|
|
@@ -111,13 +102,14 @@ import {SocketService, Nsp} from "@tsed/socketio";
|
|
|
111
102
|
|
|
112
103
|
@SocketService()
|
|
113
104
|
export class MySocketService {
|
|
114
|
-
|
|
105
|
+
@Nsp nsp: SocketIO.Namespace;
|
|
115
106
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
107
|
+
helloAll() {
|
|
108
|
+
this.nsp.emit("hi", "everyone!");
|
|
109
|
+
}
|
|
119
110
|
}
|
|
120
111
|
```
|
|
112
|
+
|
|
121
113
|
Then, you can inject your socket service into another Service, Controller, etc... as following:
|
|
122
114
|
|
|
123
115
|
```typescript
|
|
@@ -126,16 +118,13 @@ import {MySocketService} from "../services/MySocketService";
|
|
|
126
118
|
|
|
127
119
|
@Controller("/")
|
|
128
120
|
export class MyCtrl {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this.mySocketService.helloAll();
|
|
137
|
-
return "is sent";
|
|
138
|
-
}
|
|
121
|
+
constructor(private mySocketService: MySocketService) {}
|
|
122
|
+
|
|
123
|
+
@Get("/allo")
|
|
124
|
+
allo() {
|
|
125
|
+
this.mySocketService.helloAll();
|
|
126
|
+
return "is sent";
|
|
127
|
+
}
|
|
139
128
|
}
|
|
140
129
|
```
|
|
141
130
|
|
|
@@ -148,10 +137,10 @@ import {SocketService, Input, Emit, Args, Socket, Nsp} from "@tsed/socketio";
|
|
|
148
137
|
|
|
149
138
|
@SocketService("/my-namespace")
|
|
150
139
|
export class MySocketService {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
140
|
+
@Input("eventName")
|
|
141
|
+
myMethod(@Args(0) userName: string, @Socket socket: SocketIO.Socket, @Nsp nsp: SocketIO.Namespace) {
|
|
142
|
+
console.log(userName);
|
|
143
|
+
}
|
|
155
144
|
}
|
|
156
145
|
```
|
|
157
146
|
|
|
@@ -172,13 +161,14 @@ import {SocketService, Input, Emit, Args, Socket, Nsp} from "@tsed/socketio";
|
|
|
172
161
|
|
|
173
162
|
@SocketService("/my-namespace")
|
|
174
163
|
export class MySocketService {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
164
|
+
@Input("eventName")
|
|
165
|
+
@Emit("responseEventName") // or Broadcast or BroadcastOthers
|
|
166
|
+
async myMethod(@Args(0) userName: string, @Socket socket: SocketIO.Socket) {
|
|
167
|
+
return "Message " + userName;
|
|
168
|
+
}
|
|
180
169
|
}
|
|
181
170
|
```
|
|
171
|
+
|
|
182
172
|
> The method accept a promise as returned value.
|
|
183
173
|
|
|
184
174
|
::: warning
|
|
@@ -194,17 +184,16 @@ import {SocketService, Input, Emit, Args, SocketSession} from "@tsed/socketio";
|
|
|
194
184
|
|
|
195
185
|
@SocketService("/my-namespace")
|
|
196
186
|
export class MySocketService {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
187
|
+
@Input("eventName")
|
|
188
|
+
@Emit("responseEventName") // or Broadcast or BroadcastOthers
|
|
189
|
+
async myMethod(@Args(0) userName: string, @SocketSession session: SocketSession) {
|
|
190
|
+
const user = session.get("user") || {};
|
|
191
|
+
user.name = userName;
|
|
200
192
|
|
|
201
|
-
|
|
202
|
-
user.name = userName;
|
|
193
|
+
session.set("user", user);
|
|
203
194
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return user;
|
|
207
|
-
}
|
|
195
|
+
return user;
|
|
196
|
+
}
|
|
208
197
|
}
|
|
209
198
|
```
|
|
210
199
|
|
|
@@ -221,16 +210,16 @@ import {User} from "../models/User";
|
|
|
221
210
|
|
|
222
211
|
@SocketMiddleware()
|
|
223
212
|
export class UserConverterSocketMiddleware {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
213
|
+
async use(@Args() args: any[]) {
|
|
214
|
+
let [user] = args;
|
|
215
|
+
// update Arguments
|
|
216
|
+
user = deserialize(user, {type: User});
|
|
217
|
+
|
|
218
|
+
return [user];
|
|
219
|
+
}
|
|
232
220
|
}
|
|
233
221
|
```
|
|
222
|
+
|
|
234
223
|
> The user instance will be forwarded to the next middleware and to your decorated method.
|
|
235
224
|
|
|
236
225
|
You can also declare a middleware to handle an error with `@SocketMiddlewareError`.
|
|
@@ -241,10 +230,10 @@ import {SocketMiddlewareError, SocketErr, Socket} from "@tsed/socketio";
|
|
|
241
230
|
|
|
242
231
|
@SocketMiddlewareError()
|
|
243
232
|
export class ErrorHandlerSocketMiddleware {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
233
|
+
async use(@SocketErr err: any, @Socket socket: SocketIO.Socket) {
|
|
234
|
+
console.error(err);
|
|
235
|
+
socket.emit("error", {message: "An error has occured"});
|
|
236
|
+
}
|
|
248
237
|
}
|
|
249
238
|
```
|
|
250
239
|
|
|
@@ -260,7 +249,7 @@ The call sequences is the following for each event request:
|
|
|
260
249
|
- Middlewares attached with `@SocketUseBefore` on method,
|
|
261
250
|
- The method,
|
|
262
251
|
- Send response if the method is decorated with `@Emit`, `@Broadcast` or `@BroadcastOther`,
|
|
263
|
-
- Middlewares attached with `@SocketUseAfter` on method,
|
|
252
|
+
- Middlewares attached with `@SocketUseAfter` on method,
|
|
264
253
|
- Middlewares attached with `@SocketUseAfter` on class.
|
|
265
254
|
|
|
266
255
|
Middlewares chain use the `Promise` to run it. If one of this middlewares/method emit an error, the first middleware error will be called.
|
|
@@ -274,20 +263,18 @@ import {User} from "../models/User";
|
|
|
274
263
|
@SocketUseBefore(UserConverterSocketMiddleware) // global version
|
|
275
264
|
@SocketUseAfter(ErrorHandlerSocketMiddleware)
|
|
276
265
|
export class MySocketService {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
return user;
|
|
290
|
-
}
|
|
266
|
+
@Input("eventName")
|
|
267
|
+
@Emit("responseEventName") // or Broadcast or BroadcastOthers
|
|
268
|
+
@SocketUseBefore(UserConverterSocketMiddleware)
|
|
269
|
+
@SocketUseAfter(ErrorHandlerSocketMiddleware)
|
|
270
|
+
async myMethod(@Args(0) userName: User) {
|
|
271
|
+
const user = session.get("user") || {};
|
|
272
|
+
user.name = userName;
|
|
273
|
+
|
|
274
|
+
session.set("user", user);
|
|
275
|
+
|
|
276
|
+
return user;
|
|
277
|
+
}
|
|
291
278
|
}
|
|
292
279
|
```
|
|
293
280
|
|
|
@@ -296,18 +283,17 @@ export class MySocketService {
|
|
|
296
283
|
See our documentation https://tsed.io/#/api/index
|
|
297
284
|
|
|
298
285
|
## Contributors
|
|
286
|
+
|
|
299
287
|
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
300
288
|
|
|
301
289
|
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
302
290
|
|
|
303
|
-
|
|
304
291
|
## Backers
|
|
305
292
|
|
|
306
293
|
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
307
294
|
|
|
308
295
|
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
309
296
|
|
|
310
|
-
|
|
311
297
|
## Sponsors
|
|
312
298
|
|
|
313
299
|
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
@@ -8,16 +8,5 @@
|
|
|
8
8
|
"declaration": true,
|
|
9
9
|
"declarationDir": "./lib/types"
|
|
10
10
|
},
|
|
11
|
-
"exclude": [
|
|
12
|
-
"node_modules",
|
|
13
|
-
"test",
|
|
14
|
-
"lib",
|
|
15
|
-
"benchmark",
|
|
16
|
-
"coverage",
|
|
17
|
-
"spec",
|
|
18
|
-
"**/*.benchmark.ts",
|
|
19
|
-
"**/*.spec.ts",
|
|
20
|
-
"keys",
|
|
21
|
-
"jest.config.js"
|
|
22
|
-
]
|
|
11
|
+
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
|
|
23
12
|
}
|