@tsed/socketio-testing 7.0.0-alpha.1 → 7.0.0-beta.10
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/lib/{index.js → cjs/index.js} +0 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/{services → cjs/services}/SocketClientService.js +7 -7
- package/lib/cjs/services/SocketClientService.js.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/SocketClientService.js +41 -0
- package/lib/esm/services/SocketClientService.js.map +1 -0
- package/lib/{index.d.ts → types/index.d.ts} +0 -0
- package/lib/{services → types/services}/SocketClientService.d.ts +1 -1
- package/package.json +22 -12
- package/readme.md +80 -96
- package/tsconfig.compile.esm.json +12 -0
- package/lib/index.js.map +0 -1
- package/lib/services/SocketClientService.js.map +0 -1
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,yEAA+C","sourcesContent":["export * from \"./services/SocketClientService\";\n"]}
|
|
@@ -9,36 +9,36 @@ let SocketClientService = class SocketClientService {
|
|
|
9
9
|
constructor() {
|
|
10
10
|
this.clients = new Map();
|
|
11
11
|
}
|
|
12
|
-
async get(namespace = "/") {
|
|
12
|
+
async get(namespace = "/", path) {
|
|
13
13
|
if (this.clients.has(namespace)) {
|
|
14
14
|
return this.clients.get(namespace);
|
|
15
15
|
}
|
|
16
16
|
const { address, port } = this.settings.getHttpPort();
|
|
17
17
|
const uri = `http://${address}:${port}${namespace}`;
|
|
18
18
|
this.logger.info("Bind Socket.io client on:", uri);
|
|
19
|
-
const client = socket_io_client_1.io(uri);
|
|
19
|
+
const client = (0, socket_io_client_1.io)(uri, path ? { path } : undefined);
|
|
20
20
|
this.clients.set(namespace, client);
|
|
21
21
|
await new Promise((resolve) => {
|
|
22
22
|
client.on("connect", resolve);
|
|
23
23
|
});
|
|
24
|
-
this.logger.
|
|
24
|
+
this.logger.debug("Socket.io client connected...");
|
|
25
25
|
return client;
|
|
26
26
|
}
|
|
27
27
|
$onDestroy() {
|
|
28
28
|
this.clients.forEach((client) => client.close());
|
|
29
|
-
this.logger.
|
|
29
|
+
this.logger.debug("Socket.io clients closed...");
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
tslib_1.__decorate([
|
|
33
|
-
di_1.Configuration(),
|
|
33
|
+
(0, di_1.Configuration)(),
|
|
34
34
|
tslib_1.__metadata("design:type", Object)
|
|
35
35
|
], SocketClientService.prototype, "settings", void 0);
|
|
36
36
|
tslib_1.__decorate([
|
|
37
|
-
common_1.Inject(),
|
|
37
|
+
(0, common_1.Inject)(),
|
|
38
38
|
tslib_1.__metadata("design:type", common_1.Logger)
|
|
39
39
|
], SocketClientService.prototype, "logger", void 0);
|
|
40
40
|
SocketClientService = tslib_1.__decorate([
|
|
41
|
-
di_1.Injectable()
|
|
41
|
+
(0, di_1.Injectable)()
|
|
42
42
|
], SocketClientService);
|
|
43
43
|
exports.SocketClientService = SocketClientService;
|
|
44
44
|
//# sourceMappingURL=SocketClientService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SocketClientService.js","sourceRoot":"","sources":["../../../src/services/SocketClientService.ts"],"names":[],"mappings":";;;;AAAA,yCAAuD;AACvD,iCAAmD;AACnD,uDAA4C;AAG5C,IAAa,mBAAmB,GAAhC,MAAa,mBAAmB;IAAhC;QAOU,YAAO,GAAwB,IAAI,GAAG,EAAE,CAAC;IA4BnD,CAAC;IA1BC,KAAK,CAAC,GAAG,CAAC,YAAoB,GAAG,EAAE,IAAa;QAC9C,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;YAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAE,CAAC;SACrC;QAED,MAAM,EAAC,OAAO,EAAE,IAAI,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,UAAU,OAAO,IAAI,IAAI,GAAG,SAAS,EAAE,CAAC;QAEpD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAC;QACnD,MAAM,MAAM,GAAG,IAAA,qBAAE,EAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,EAAC,IAAI,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QAElD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAEpC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,OAAc,CAAC,CAAC;QACvC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC;QAEnD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,UAAU;QACR,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACnD,CAAC;CACF,CAAA;AAjCC;IADC,IAAA,kBAAa,GAAE;;qDACgB;AAGhC;IADC,IAAA,eAAM,GAAE;sCACO,eAAM;mDAAC;AALZ,mBAAmB;IAD/B,IAAA,eAAU,GAAE;GACA,mBAAmB,CAmC/B;AAnCY,kDAAmB","sourcesContent":["import {Inject, Logger, OnDestroy} from \"@tsed/common\";\nimport {Configuration, Injectable} from \"@tsed/di\";\nimport {io, Socket} from \"socket.io-client\";\n\n@Injectable()\nexport class SocketClientService implements OnDestroy {\n @Configuration()\n private settings: Configuration;\n\n @Inject()\n private logger: Logger;\n\n private clients: Map<string, Socket> = new Map();\n\n async get(namespace: string = \"/\", path?: string): Promise<Socket> {\n if (this.clients.has(namespace)) {\n return this.clients.get(namespace)!;\n }\n\n const {address, port} = this.settings.getHttpPort();\n const uri = `http://${address}:${port}${namespace}`;\n\n this.logger.info(\"Bind Socket.io client on:\", uri);\n const client = io(uri, path ? {path} : undefined);\n\n this.clients.set(namespace, client);\n\n await new Promise((resolve) => {\n client.on(\"connect\", resolve as any);\n });\n\n this.logger.debug(\"Socket.io client connected...\");\n\n return client;\n }\n\n $onDestroy() {\n this.clients.forEach((client) => client.close());\n this.logger.debug(\"Socket.io clients closed...\");\n }\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC","sourcesContent":["export * from \"./services/SocketClientService\";\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Inject, Logger } from "@tsed/common";
|
|
3
|
+
import { Configuration, Injectable } from "@tsed/di";
|
|
4
|
+
import { io } from "socket.io-client";
|
|
5
|
+
let SocketClientService = class SocketClientService {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.clients = new Map();
|
|
8
|
+
}
|
|
9
|
+
async get(namespace = "/", path) {
|
|
10
|
+
if (this.clients.has(namespace)) {
|
|
11
|
+
return this.clients.get(namespace);
|
|
12
|
+
}
|
|
13
|
+
const { address, port } = this.settings.getHttpPort();
|
|
14
|
+
const uri = `http://${address}:${port}${namespace}`;
|
|
15
|
+
this.logger.info("Bind Socket.io client on:", uri);
|
|
16
|
+
const client = io(uri, path ? { path } : undefined);
|
|
17
|
+
this.clients.set(namespace, client);
|
|
18
|
+
await new Promise((resolve) => {
|
|
19
|
+
client.on("connect", resolve);
|
|
20
|
+
});
|
|
21
|
+
this.logger.debug("Socket.io client connected...");
|
|
22
|
+
return client;
|
|
23
|
+
}
|
|
24
|
+
$onDestroy() {
|
|
25
|
+
this.clients.forEach((client) => client.close());
|
|
26
|
+
this.logger.debug("Socket.io clients closed...");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
__decorate([
|
|
30
|
+
Configuration(),
|
|
31
|
+
__metadata("design:type", Object)
|
|
32
|
+
], SocketClientService.prototype, "settings", void 0);
|
|
33
|
+
__decorate([
|
|
34
|
+
Inject(),
|
|
35
|
+
__metadata("design:type", Logger)
|
|
36
|
+
], SocketClientService.prototype, "logger", void 0);
|
|
37
|
+
SocketClientService = __decorate([
|
|
38
|
+
Injectable()
|
|
39
|
+
], SocketClientService);
|
|
40
|
+
export { SocketClientService };
|
|
41
|
+
//# sourceMappingURL=SocketClientService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SocketClientService.js","sourceRoot":"","sources":["../../../src/services/SocketClientService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,MAAM,EAAY,MAAM,cAAc,CAAC;AACvD,OAAO,EAAC,aAAa,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,EAAE,EAAS,MAAM,kBAAkB,CAAC;AAG5C,IAAa,mBAAmB,GAAhC,MAAa,mBAAmB;IAAhC;QAOU,YAAO,GAAwB,IAAI,GAAG,EAAE,CAAC;IA4BnD,CAAC;IA1BC,KAAK,CAAC,GAAG,CAAC,YAAoB,GAAG,EAAE,IAAa;QAC9C,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;YAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAE,CAAC;SACrC;QAED,MAAM,EAAC,OAAO,EAAE,IAAI,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,UAAU,OAAO,IAAI,IAAI,GAAG,SAAS,EAAE,CAAC;QAEpD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAC;QACnD,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,EAAC,IAAI,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QAElD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAEpC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,OAAc,CAAC,CAAC;QACvC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC;QAEnD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,UAAU;QACR,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACnD,CAAC;CACF,CAAA;AAjCC;IADC,aAAa,EAAE;;qDACgB;AAGhC;IADC,MAAM,EAAE;8BACO,MAAM;mDAAC;AALZ,mBAAmB;IAD/B,UAAU,EAAE;GACA,mBAAmB,CAmC/B;SAnCY,mBAAmB","sourcesContent":["import {Inject, Logger, OnDestroy} from \"@tsed/common\";\nimport {Configuration, Injectable} from \"@tsed/di\";\nimport {io, Socket} from \"socket.io-client\";\n\n@Injectable()\nexport class SocketClientService implements OnDestroy {\n @Configuration()\n private settings: Configuration;\n\n @Inject()\n private logger: Logger;\n\n private clients: Map<string, Socket> = new Map();\n\n async get(namespace: string = \"/\", path?: string): Promise<Socket> {\n if (this.clients.has(namespace)) {\n return this.clients.get(namespace)!;\n }\n\n const {address, port} = this.settings.getHttpPort();\n const uri = `http://${address}:${port}${namespace}`;\n\n this.logger.info(\"Bind Socket.io client on:\", uri);\n const client = io(uri, path ? {path} : undefined);\n\n this.clients.set(namespace, client);\n\n await new Promise((resolve) => {\n client.on(\"connect\", resolve as any);\n });\n\n this.logger.debug(\"Socket.io client connected...\");\n\n return client;\n }\n\n $onDestroy() {\n this.clients.forEach((client) => client.close());\n this.logger.debug(\"Socket.io clients closed...\");\n }\n}\n"]}
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/socketio-testing",
|
|
3
|
-
"version": "7.0.0-
|
|
3
|
+
"version": "7.0.0-beta.10",
|
|
4
4
|
"description": "Socket.io testing package for Ts.ED framework",
|
|
5
|
-
"main": "./lib/index.js",
|
|
6
|
-
"typings": "./lib/index.d.ts",
|
|
7
5
|
"private": false,
|
|
6
|
+
"source": "./src/index.ts",
|
|
7
|
+
"main": "./lib/cjs/index.js",
|
|
8
|
+
"module": "./lib/esm/index.js",
|
|
9
|
+
"typings": "./lib/types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
"types": "./lib/types/index.d.ts",
|
|
12
|
+
"import": "./lib/esm/index.js",
|
|
13
|
+
"require": "./lib/cjs/index.js",
|
|
14
|
+
"default": "./lib/esm/index.js"
|
|
15
|
+
},
|
|
8
16
|
"scripts": {
|
|
9
|
-
"build": "
|
|
17
|
+
"build": "yarn run build:esm && yarn run build:cjs",
|
|
18
|
+
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
19
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
10
20
|
"start": "ts-node -r tsconfig-paths/register test/app/index.ts"
|
|
11
21
|
},
|
|
12
22
|
"dependencies": {
|
|
13
|
-
"tslib": "2.
|
|
23
|
+
"tslib": "2.3.1"
|
|
14
24
|
},
|
|
15
25
|
"devDependencies": {
|
|
16
|
-
"@tsed/common": "7.0.0-
|
|
17
|
-
"@tsed/di": "7.0.0-
|
|
26
|
+
"@tsed/common": "7.0.0-beta.10",
|
|
27
|
+
"@tsed/di": "7.0.0-beta.10",
|
|
18
28
|
"socket.io-client": "^4.1.2"
|
|
19
29
|
},
|
|
20
30
|
"peerDependencies": {
|
|
21
|
-
"@tsed/common": "^7.0.0-
|
|
22
|
-
"@tsed/core": "^7.0.0-
|
|
23
|
-
"@tsed/di": "^7.0.0-
|
|
24
|
-
"@tsed/socketio": "^7.0.0-
|
|
31
|
+
"@tsed/common": "^7.0.0-beta.10",
|
|
32
|
+
"@tsed/core": "^7.0.0-beta.10",
|
|
33
|
+
"@tsed/di": "^7.0.0-beta.10",
|
|
34
|
+
"@tsed/socketio": "^7.0.0-beta.10",
|
|
25
35
|
"socket.io-client": "^4.0.1"
|
|
26
36
|
},
|
|
27
37
|
"repository": "https://github.com/tsedio/tsed",
|
|
28
38
|
"bugs": {
|
|
29
39
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
30
40
|
},
|
|
31
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
41
|
+
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/third-parties/socketio-testing",
|
|
32
42
|
"author": "Romain Lenzotti",
|
|
33
43
|
"license": "MIT"
|
|
34
44
|
}
|
package/readme.md
CHANGED
|
@@ -44,24 +44,20 @@ 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)
|
|
48
47
|
|
|
49
48
|
@Configuration({
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
49
|
+
socketIO: {
|
|
50
|
+
// ... see configuration
|
|
51
|
+
}
|
|
54
52
|
})
|
|
55
|
-
export class Server {
|
|
56
|
-
|
|
57
|
-
}
|
|
53
|
+
export class Server {}
|
|
58
54
|
```
|
|
59
55
|
|
|
60
56
|
## Socket Service
|
|
61
57
|
|
|
62
58
|
> 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/#).
|
|
59
|
+
> This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application
|
|
60
|
+
> by introducing separation between communication channels. See [namespace documentation](https://socket.io/docs/rooms-and-namespaces/#).
|
|
65
61
|
|
|
66
62
|
All Socket service work under a namespace and you can create one Socket service per namespace.
|
|
67
63
|
|
|
@@ -73,31 +69,24 @@ import {SocketService, IO, Nsp, Socket, SocketSession} from "@tsed/socketio";
|
|
|
73
69
|
|
|
74
70
|
@SocketService("/my-namespace")
|
|
75
71
|
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
|
-
}
|
|
72
|
+
@Nsp nsp: SocketIO.Namespace;
|
|
73
|
+
|
|
74
|
+
@Nsp("/my-other-namespace")
|
|
75
|
+
nspOther: SocketIO.Namespace; // communication between two namespace
|
|
76
|
+
|
|
77
|
+
constructor(@IO private io: SocketIO.Server) {}
|
|
78
|
+
/**
|
|
79
|
+
* Triggered the namespace is created
|
|
80
|
+
*/
|
|
81
|
+
$onNamespaceInit(nsp: SocketIO.Namespace) {}
|
|
82
|
+
/**
|
|
83
|
+
* Triggered when a new client connects to the Namespace.
|
|
84
|
+
*/
|
|
85
|
+
$onConnection(@Socket socket: SocketIO.Socket, @SocketSession session: SocketSession) {}
|
|
86
|
+
/**
|
|
87
|
+
* Triggered when a client disconnects from the Namespace.
|
|
88
|
+
*/
|
|
89
|
+
$onDisconnect(@Socket socket: SocketIO.Socket) {}
|
|
101
90
|
}
|
|
102
91
|
```
|
|
103
92
|
|
|
@@ -111,13 +100,14 @@ import {SocketService, Nsp} from "@tsed/socketio";
|
|
|
111
100
|
|
|
112
101
|
@SocketService()
|
|
113
102
|
export class MySocketService {
|
|
114
|
-
|
|
103
|
+
@Nsp nsp: SocketIO.Namespace;
|
|
115
104
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
105
|
+
helloAll() {
|
|
106
|
+
this.nsp.emit("hi", "everyone!");
|
|
107
|
+
}
|
|
119
108
|
}
|
|
120
109
|
```
|
|
110
|
+
|
|
121
111
|
Then, you can inject your socket service into another Service, Controller, etc... as following:
|
|
122
112
|
|
|
123
113
|
```typescript
|
|
@@ -126,16 +116,13 @@ import {MySocketService} from "../services/MySocketService";
|
|
|
126
116
|
|
|
127
117
|
@Controller("/")
|
|
128
118
|
export class MyCtrl {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this.mySocketService.helloAll();
|
|
137
|
-
return "is sent";
|
|
138
|
-
}
|
|
119
|
+
constructor(private mySocketService: MySocketService) {}
|
|
120
|
+
|
|
121
|
+
@Get("/allo")
|
|
122
|
+
allo() {
|
|
123
|
+
this.mySocketService.helloAll();
|
|
124
|
+
return "is sent";
|
|
125
|
+
}
|
|
139
126
|
}
|
|
140
127
|
```
|
|
141
128
|
|
|
@@ -148,10 +135,10 @@ import {SocketService, Input, Emit, Args, Socket, Nsp} from "@tsed/socketio";
|
|
|
148
135
|
|
|
149
136
|
@SocketService("/my-namespace")
|
|
150
137
|
export class MySocketService {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
138
|
+
@Input("eventName")
|
|
139
|
+
myMethod(@Args(0) userName: string, @Socket socket: SocketIO.Socket, @Nsp nsp: SocketIO.Namespace) {
|
|
140
|
+
console.log(userName);
|
|
141
|
+
}
|
|
155
142
|
}
|
|
156
143
|
```
|
|
157
144
|
|
|
@@ -172,13 +159,14 @@ import {SocketService, Input, Emit, Args, Socket, Nsp} from "@tsed/socketio";
|
|
|
172
159
|
|
|
173
160
|
@SocketService("/my-namespace")
|
|
174
161
|
export class MySocketService {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
162
|
+
@Input("eventName")
|
|
163
|
+
@Emit("responseEventName") // or Broadcast or BroadcastOthers
|
|
164
|
+
async myMethod(@Args(0) userName: string, @Socket socket: SocketIO.Socket) {
|
|
165
|
+
return "Message " + userName;
|
|
166
|
+
}
|
|
180
167
|
}
|
|
181
168
|
```
|
|
169
|
+
|
|
182
170
|
> The method accept a promise as returned value.
|
|
183
171
|
|
|
184
172
|
::: warning
|
|
@@ -194,17 +182,16 @@ import {SocketService, Input, Emit, Args, SocketSession} from "@tsed/socketio";
|
|
|
194
182
|
|
|
195
183
|
@SocketService("/my-namespace")
|
|
196
184
|
export class MySocketService {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
185
|
+
@Input("eventName")
|
|
186
|
+
@Emit("responseEventName") // or Broadcast or BroadcastOthers
|
|
187
|
+
async myMethod(@Args(0) userName: string, @SocketSession session: SocketSession) {
|
|
188
|
+
const user = session.get("user") || {};
|
|
189
|
+
user.name = userName;
|
|
200
190
|
|
|
201
|
-
|
|
202
|
-
user.name = userName;
|
|
191
|
+
session.set("user", user);
|
|
203
192
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return user;
|
|
207
|
-
}
|
|
193
|
+
return user;
|
|
194
|
+
}
|
|
208
195
|
}
|
|
209
196
|
```
|
|
210
197
|
|
|
@@ -221,16 +208,16 @@ import {User} from "../models/User";
|
|
|
221
208
|
|
|
222
209
|
@SocketMiddleware()
|
|
223
210
|
export class UserConverterSocketMiddleware {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
211
|
+
async use(@Args() args: any[]) {
|
|
212
|
+
let [user] = args;
|
|
213
|
+
// update Arguments
|
|
214
|
+
user = deserialize(user, {type: User});
|
|
215
|
+
|
|
216
|
+
return [user];
|
|
217
|
+
}
|
|
232
218
|
}
|
|
233
219
|
```
|
|
220
|
+
|
|
234
221
|
> The user instance will be forwarded to the next middleware and to your decorated method.
|
|
235
222
|
|
|
236
223
|
You can also declare a middleware to handle an error with `@SocketMiddlewareError`.
|
|
@@ -241,10 +228,10 @@ import {SocketMiddlewareError, SocketErr, Socket} from "@tsed/socketio";
|
|
|
241
228
|
|
|
242
229
|
@SocketMiddlewareError()
|
|
243
230
|
export class ErrorHandlerSocketMiddleware {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
231
|
+
async use(@SocketErr err: any, @Socket socket: SocketIO.Socket) {
|
|
232
|
+
console.error(err);
|
|
233
|
+
socket.emit("error", {message: "An error has occured"});
|
|
234
|
+
}
|
|
248
235
|
}
|
|
249
236
|
```
|
|
250
237
|
|
|
@@ -260,7 +247,7 @@ The call sequences is the following for each event request:
|
|
|
260
247
|
- Middlewares attached with `@SocketUseBefore` on method,
|
|
261
248
|
- The method,
|
|
262
249
|
- Send response if the method is decorated with `@Emit`, `@Broadcast` or `@BroadcastOther`,
|
|
263
|
-
- Middlewares attached with `@SocketUseAfter` on method,
|
|
250
|
+
- Middlewares attached with `@SocketUseAfter` on method,
|
|
264
251
|
- Middlewares attached with `@SocketUseAfter` on class.
|
|
265
252
|
|
|
266
253
|
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 +261,18 @@ import {User} from "../models/User";
|
|
|
274
261
|
@SocketUseBefore(UserConverterSocketMiddleware) // global version
|
|
275
262
|
@SocketUseAfter(ErrorHandlerSocketMiddleware)
|
|
276
263
|
export class MySocketService {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
return user;
|
|
290
|
-
}
|
|
264
|
+
@Input("eventName")
|
|
265
|
+
@Emit("responseEventName") // or Broadcast or BroadcastOthers
|
|
266
|
+
@SocketUseBefore(UserConverterSocketMiddleware)
|
|
267
|
+
@SocketUseAfter(ErrorHandlerSocketMiddleware)
|
|
268
|
+
async myMethod(@Args(0) userName: User) {
|
|
269
|
+
const user = session.get("user") || {};
|
|
270
|
+
user.name = userName;
|
|
271
|
+
|
|
272
|
+
session.set("user", user);
|
|
273
|
+
|
|
274
|
+
return user;
|
|
275
|
+
}
|
|
291
276
|
}
|
|
292
277
|
```
|
|
293
278
|
|
|
@@ -296,18 +281,17 @@ export class MySocketService {
|
|
|
296
281
|
See our documentation https://tsed.io/#/api/index
|
|
297
282
|
|
|
298
283
|
## Contributors
|
|
284
|
+
|
|
299
285
|
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
300
286
|
|
|
301
287
|
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
302
288
|
|
|
303
|
-
|
|
304
289
|
## Backers
|
|
305
290
|
|
|
306
291
|
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
307
292
|
|
|
308
293
|
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
309
294
|
|
|
310
|
-
|
|
311
295
|
## Sponsors
|
|
312
296
|
|
|
313
297
|
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)]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.compile.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"module": "ES2020",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "./lib/esm",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationDir": "./lib/types"
|
|
10
|
+
},
|
|
11
|
+
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
|
|
12
|
+
}
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yEAA+C","sourcesContent":["export * from \"./services/SocketClientService\";\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SocketClientService.js","sourceRoot":"","sources":["../../src/services/SocketClientService.ts"],"names":[],"mappings":";;;;AAAA,yCAAuD;AACvD,iCAAmD;AACnD,uDAA4C;AAG5C,IAAa,mBAAmB,GAAhC,MAAa,mBAAmB;IAAhC;QAOU,YAAO,GAAwB,IAAI,GAAG,EAAE,CAAC;IA4BnD,CAAC;IA1BC,KAAK,CAAC,GAAG,CAAC,YAAoB,GAAG;QAC/B,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE;YAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAE,CAAC;SACrC;QAED,MAAM,EAAC,OAAO,EAAE,IAAI,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,UAAU,OAAO,IAAI,IAAI,GAAG,SAAS,EAAE,CAAC;QAEpD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAC;QACnD,MAAM,MAAM,GAAG,qBAAE,CAAC,GAAG,CAAC,CAAC;QAEvB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAEpC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,OAAc,CAAC,CAAC;QACvC,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;QAElD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,UAAU;QACR,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;IAClD,CAAC;CACF,CAAA;AAjCC;IADC,kBAAa,EAAE;;qDACgB;AAGhC;IADC,eAAM,EAAE;sCACO,eAAM;mDAAC;AALZ,mBAAmB;IAD/B,eAAU,EAAE;GACA,mBAAmB,CAmC/B;AAnCY,kDAAmB","sourcesContent":["import {Inject, Logger, OnDestroy} from \"@tsed/common\";\nimport {Configuration, Injectable} from \"@tsed/di\";\nimport {io, Socket} from \"socket.io-client\";\n\n@Injectable()\nexport class SocketClientService implements OnDestroy {\n @Configuration()\n private settings: Configuration;\n\n @Inject()\n private logger: Logger;\n\n private clients: Map<string, Socket> = new Map();\n\n async get(namespace: string = \"/\"): Promise<Socket> {\n if (this.clients.has(namespace)) {\n return this.clients.get(namespace)!;\n }\n\n const {address, port} = this.settings.getHttpPort();\n const uri = `http://${address}:${port}${namespace}`;\n\n this.logger.info(\"Bind Socket.io client on:\", uri);\n const client = io(uri);\n\n this.clients.set(namespace, client);\n\n await new Promise((resolve) => {\n client.on(\"connect\", resolve as any);\n });\n\n this.logger.info(\"Socket.io client connected...\");\n\n return client;\n }\n\n $onDestroy() {\n this.clients.forEach((client) => client.close());\n this.logger.info(\"Socket.io clients closed...\");\n }\n}\n"]}
|