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