ts-game-decorators 1.0.26 → 1.0.28
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.
|
@@ -3,7 +3,7 @@ import { Server } from 'socket.io';
|
|
|
3
3
|
export declare function AuthenSocket(): MethodDecorator & ClassDecorator;
|
|
4
4
|
export declare function SocketService(): ClassDecorator;
|
|
5
5
|
export declare function OnEvent(event: string): MethodDecorator;
|
|
6
|
+
export declare function OnConnect(): MethodDecorator;
|
|
6
7
|
export declare function OnDisconnect(): MethodDecorator;
|
|
7
8
|
export declare function OnError(): MethodDecorator;
|
|
8
|
-
export declare function OnConnect(): MethodDecorator;
|
|
9
9
|
export declare function registerSocketServices(io: Server, serviceClasses: any[], authSocketMiddleware?: (socket: any, next: (err?: Error) => void) => void): void;
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AuthenSocket = AuthenSocket;
|
|
4
4
|
exports.SocketService = SocketService;
|
|
5
5
|
exports.OnEvent = OnEvent;
|
|
6
|
+
exports.OnConnect = OnConnect;
|
|
6
7
|
exports.OnDisconnect = OnDisconnect;
|
|
7
8
|
exports.OnError = OnError;
|
|
8
|
-
exports.OnConnect = OnConnect;
|
|
9
9
|
exports.registerSocketServices = registerSocketServices;
|
|
10
10
|
require("reflect-metadata");
|
|
11
11
|
const SOCKET_SERVICE_META = Symbol('socket_service');
|
|
@@ -38,24 +38,24 @@ function OnEvent(event) {
|
|
|
38
38
|
Reflect.defineMetadata(SOCKET_EVENTS_META, events, target.constructor);
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
-
function
|
|
41
|
+
function OnConnect() {
|
|
42
42
|
return (target, propertyKey, descriptor) => {
|
|
43
43
|
const events = Reflect.getMetadata(SOCKET_EVENTS_META, target.constructor) || [];
|
|
44
|
-
events.push({ type: '
|
|
44
|
+
events.push({ type: 'connect', handler: propertyKey });
|
|
45
45
|
Reflect.defineMetadata(SOCKET_EVENTS_META, events, target.constructor);
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
|
-
function
|
|
48
|
+
function OnDisconnect() {
|
|
49
49
|
return (target, propertyKey, descriptor) => {
|
|
50
50
|
const events = Reflect.getMetadata(SOCKET_EVENTS_META, target.constructor) || [];
|
|
51
|
-
events.push({ type: '
|
|
51
|
+
events.push({ type: 'disconnect', handler: propertyKey });
|
|
52
52
|
Reflect.defineMetadata(SOCKET_EVENTS_META, events, target.constructor);
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
|
-
function
|
|
55
|
+
function OnError() {
|
|
56
56
|
return (target, propertyKey, descriptor) => {
|
|
57
57
|
const events = Reflect.getMetadata(SOCKET_EVENTS_META, target.constructor) || [];
|
|
58
|
-
events.push({ type: '
|
|
58
|
+
events.push({ type: 'error', handler: propertyKey });
|
|
59
59
|
Reflect.defineMetadata(SOCKET_EVENTS_META, events, target.constructor);
|
|
60
60
|
};
|
|
61
61
|
}
|
|
@@ -101,6 +101,20 @@ function registerSocketServices(io, serviceClasses, authSocketMiddleware) {
|
|
|
101
101
|
}
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
|
+
else if (evt.type === 'connect') {
|
|
105
|
+
// Gọi ngay khi client connect vào, nhưng chạy auth trước nếu cần
|
|
106
|
+
if (needAuth) {
|
|
107
|
+
const authFn = authSocketMiddleware || authSocketToken;
|
|
108
|
+
authFn(socket, (err) => {
|
|
109
|
+
if (err)
|
|
110
|
+
return socket.emit('server:error', err.message);
|
|
111
|
+
Promise.resolve(instance[evt.handler](socket)).catch(console.error);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
Promise.resolve(instance[evt.handler](socket)).catch(console.error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
104
118
|
else if (evt.type === 'disconnect') {
|
|
105
119
|
socket.on('disconnect', (...args) => handler(...args));
|
|
106
120
|
}
|
package/package.json
CHANGED