1
import {SocketIOStatic} from "../index";
2
import {ISocketHandler} from "../interfaces/socket-handler.interface";
3 100
import {NamespaceHandler} from "../classes/namespace-handler.class";
4
import {ISocketMiddleware} from "../interfaces/socket-middleware.interface";
5
import {ISocketServerMiddleware} from "../interfaces/socket-server-middleware.interface";
6
import {IExtendedSocket} from "../interfaces/extended-socket.interface";
7

8 100
export enum HandlerType {
9 100
    SERVER = "SERVER",
10 100
    SOCKET = "SOCKET"
11
}
12

13

14 100
export class SocketHandler implements ISocketHandler {
15 100
    public static readonly COUNTER_START = 1;
16 100
    public static readonly IDPrefix = "SUG_SOCKET_HANDLER_";
17 100
    private static counter = SocketHandler.COUNTER_START;
18

19 100
    protected static socketHandlers: Map<Symbol, SocketHandler> = new Map();
20 100
    protected static _handlersIds: Map<Symbol, string> = new Map();
21 100
    protected static _pendingMap: { [prop: string]: Map<string, Array<any>> } = {
22
        [HandlerType.SERVER]: new Map(),
23
        [HandlerType.SOCKET]: new Map()
24
    };
25

26 100
    private _namespaces: Map<string, NamespaceHandler> = new Map();
27
    private _socketServer: IOptimizedServerInstance;
28

29

30
    public static init(port: number): SocketIOStatic.Server
31
    public static init(port: number, socketConfig: SocketIOStatic.ServerOptions): SocketIOStatic.Server
32
    public static init(port: number, socketConfig: SocketIOStatic.ServerOptions, namespace: string): SocketIOStatic.Server
33
    public static init(HttpServer: any): SocketIOStatic.Server
34
    public static init(HttpServer: any, socketConfig: SocketIOStatic.ServerOptions): SocketIOStatic.Server
35
    public static init(HttpServer: any, socketConfig: SocketIOStatic.ServerOptions, namespace: string): SocketIOStatic.Server
36
    public static init(HttpServer: any,
37 100
                       socketConfig: SocketIOStatic.ServerOptions = {},
38 100
                       namespace: string = "/"): SocketIOStatic.Server {
39 100
        const serverSymbol = Symbol.for(HttpServer);
40 100
        const id = SocketHandler._handlersIds.has(serverSymbol)
41 100
        ? SocketHandler._handlersIds.get(serverSymbol)
42 100
        : SocketHandler.IDPrefix + this.counter++;
43
        let socketHandler;
44 100
        if (this.socketHandlers.has(Symbol.for(id))) {
45 100
            socketHandler = this.socketHandlers.get(Symbol.for(id));
46 100
            socketHandler.addNamespace(namespace);
47
        } else {
48 100
            socketHandler = new this(HttpServer, id, socketConfig, namespace);
49 100
            this.socketHandlers.set(Symbol.for(id), socketHandler);
50 100
            SocketHandler._handlersIds.set(serverSymbol, id);
51
        }
52 100
        return socketHandler.getServer();
53
    }
54

55
    public static getHandler(): SocketHandler
56
    public static getHandler(id: string): SocketHandler
57 100
    public static getHandler(id: string = null): SocketHandler {
58 100
        if (id) {
59 100
            return this.socketHandlers.get(Symbol.for(id));
60
        }
61 100
        else if (this.socketHandlers.size > 0) {
62 100
            return this.socketHandlers.get(Symbol.for(SocketHandler.IDPrefix + SocketHandler.COUNTER_START));
63
        } else {
64 100
            return null;
65
        }
66
    }
67

68 100
    protected constructor(HttpServer, id: string, socketConfig: SocketIOStatic.ServerOptions, namespace: string = "/") {
69 100
        this._socketServer = require('socket.io')(HttpServer, socketConfig);
70 100
        this._socketServer.instanceId = id;
71 100
        this._socketServer.getInstanceId = function () {
72 100
            return this.instanceId;
73
        };
74 100
        this.addNamespace(namespace)
75
    }
76

77
    addNamespace(namespace: string): NamespaceHandler;
78
    addNamespace(namespace: string, ...middlewares: Array<ISocketServerMiddleware>): NamespaceHandler;
79 100
    addNamespace(namespace: string, ...middlewares: Array<ISocketServerMiddleware>): NamespaceHandler {
80 100
        if (!(!!namespace && !this._namespaces.has(namespace))) return;
81

82 100
        const namespaceServer = this._socketServer.of(namespace);
83 100
        const namespaceHandler = new NamespaceHandler(namespace, namespaceServer, ...middlewares);
84 100
        if (SocketHandler._pendingMap[HandlerType.SOCKET].has(namespace)) {
85 100
            SocketHandler._pendingMap[HandlerType.SOCKET].get(namespace).forEach(item => {
86 100
                namespaceHandler.registerSocketEvent(item.event, item.callback, ...item.middlewares)
87
            });
88 100
            SocketHandler._pendingMap[HandlerType.SOCKET].delete(namespace);
89
        }
90 100
        if (SocketHandler._pendingMap[HandlerType.SERVER].has(namespace)) {
91 100
            SocketHandler._pendingMap[HandlerType.SERVER].get(namespace).forEach(item => {
92 100
                namespaceHandler.registerServerEvent(item.event, item.callback, ...item.middlewares)
93
            });
94 100
            SocketHandler._pendingMap[HandlerType.SERVER].delete(namespace);
95
        }
96 100
        this._namespaces.set(namespace, namespaceHandler);
97 100
        return namespaceHandler;
98
    }
99

100

101
    static RegisterEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void, namespace: string);
102
    static RegisterEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>);
103
    static RegisterEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>, namespace: string);
104 100
    static RegisterEvent<T=any>(handlerType: HandlerType, event, callback, middlewares?: Array<any> | string, namespace: string = "/") {
105 100
        if (typeof middlewares === "string") {
106 0
            namespace = middlewares as string;
107 0
            middlewares = []
108
        }
109 100
        const namespaceArray = this._pendingMap[handlerType].get(namespace) || [];
110 100
        namespaceArray.push({event, callback, middlewares: middlewares || []});
111 100
        this._pendingMap[handlerType].set(namespace, namespaceArray);
112
    };
113

114

115
    registerEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void);
116
    registerEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void, namespace: string);
117
    registerEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>);
118
    registerEvent<T>(handlerType: HandlerType, event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>, namespace: string);
119 100
    registerEvent<T=any>(handlerType: HandlerType, event, callback, middlewares: Array<any> | string = [], namespace: string = null) {
120 100
        if (typeof middlewares === "string") {
121 0
            namespace = middlewares as string;
122 0
            middlewares = []
123 100
        } else if (!namespace) {
124 0
            namespace = "/"
125
        }
126 100
        let handler = this.getNamespaceHandler(namespace);
127 100
        if (!handler && !(handler = this.addNamespace(namespace)))
128 0
            SocketHandler.RegisterEvent(handlerType, event, callback, middlewares, namespace);
129
        else
130 100
            handler.registerEvent(handlerType, event, callback, ...middlewares);
131
    }
132

133
    registerSocketEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void);
134
    registerSocketEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void, namespace: string);
135
    registerSocketEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>);
136
    registerSocketEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>, namespace: string);
137 100
    registerSocketEvent<T=any>(event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares?: Array<ISocketMiddleware> | string, namespace: string = "/") {
138 100
        this.registerEvent(HandlerType.SOCKET, event, callback, middlewares as Array<ISocketMiddleware>, namespace);
139
    }
140

141
    registerServerEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void);
142
    registerServerEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void, namespace: string);
143
    registerServerEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>);
144
    registerServerEvent<T>(event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares: Array<ISocketMiddleware>, namespace: string);
145 100
    registerServerEvent<T=any>(event: string, callback: (socket: IExtendedSocket, data: T) => void, middlewares?: Array<ISocketMiddleware> | string, namespace: string = "/") {
146 0
        this.registerEvent(HandlerType.SERVER, event, callback, middlewares as Array<ISocketMiddleware>, namespace);
147
    }
148

149

150

151
    deregisterSocketEvent(event: string);
152
    deregisterSocketEvent(event: string, functionToRemove: (...args) => void);
153
    deregisterSocketEvent(event: string, functionToRemove: (...args) => void, namespace: string);
154 100
    deregisterSocketEvent(event, functionToRemove: (...args) => void = null, namespace = "/") {
155 100
        const handler = this.getNamespaceHandler(namespace);
156 100
        if (handler)
157 100
            handler.deregisterSocketEvent( event, functionToRemove);
158
    }
159

160

161
    deregisterServerEvent(event: string);
162
    deregisterServerEvent(event: string, functionToRemove: (...args) => void);
163
    deregisterServerEvent(event: string, functionToRemove: (...args) => void, namespace: string);
164 100
    deregisterServerEvent(event, functionToRemove: (...args) => void = null, namespace = "/") {
165 0
        const handler = this.getNamespaceHandler(namespace);
166 100
        if (handler)
167 0
            handler.deregisterServerEvent(event, functionToRemove);
168
    }
169

170 100
    getNamespace(namespace: string): SocketIOStatic.Namespace {
171 100
        return this._namespaces.has(namespace)
172 100
            ? this._namespaces.get(namespace).instance
173 0
            : null;
174
    }
175

176 100
    getServer(): IOptimizedServerInstance {
177 100
        return this._socketServer;
178
    }
179

180 100
    private getNamespaceHandler(namespace: string): NamespaceHandler {
181 100
        return this._namespaces.get(namespace);
182
    }
183

184
}
185

186

187
export type IOptimizedServerInstance = SocketIOStatic.Server & {
188
    getInstanceId(): string;
189
    instanceId: string;
190
};

Read our documentation on viewing source code .

Loading