-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added extra args for handlers in WS
- Loading branch information
Showing
2 changed files
with
29 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export class Router { | ||
static protocols = new Map<string, { handler: Function; }>(); | ||
|
||
static registerHandler(protocol: string, handler: Function): void { | ||
this.protocols.set(protocol, { handler }); | ||
} | ||
|
||
static getProtocol(protocol: string): { handler: Function; } { | ||
return this.protocols.get(protocol); | ||
} | ||
|
||
static async handle(protocol: string, ...args): Promise<void> { | ||
await this.getProtocol(protocol).handler(...args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,41 @@ | ||
import type * as FN from '@soketi/impl/types'; | ||
import { Router as BaseRouter } from '../router'; | ||
|
||
export class Router { | ||
static protocols = new Map<string, { handler: Function; }>(); | ||
|
||
export class Router extends BaseRouter { | ||
static ON_NEW_CONNECTION = 'onNewConnection'; | ||
static ON_CONNECTION_CLOSED = 'onConnectionClosed'; | ||
static ON_MESSAGE = 'onMessage'; | ||
static ON_ERROR = 'onError'; | ||
|
||
static registerHandler(protocol: string, handler: Function): void { | ||
this.protocols.set(protocol, { handler }); | ||
} | ||
|
||
static onNewConnection(cb: (connection: FN.WS.Connection) => any): void { | ||
static onNewConnection(cb: (connection: FN.WS.Connection, ...args) => any): void { | ||
this.registerHandler(this.ON_NEW_CONNECTION, cb); | ||
} | ||
|
||
static onConnectionClosed(cb: (connection: FN.WS.Connection, code?: number, message?: any) => any): void { | ||
static onConnectionClosed(cb: (connection: FN.WS.Connection, code?: number, message?: any, ...args) => any): void { | ||
this.registerHandler(this.ON_CONNECTION_CLOSED, cb); | ||
} | ||
|
||
static onMessage(cb: (connection: FN.WS.Connection, message: any) => any): void { | ||
static onMessage(cb: (connection: FN.WS.Connection, message: any, ...args) => any): void { | ||
this.registerHandler(this.ON_MESSAGE, cb); | ||
} | ||
|
||
static onError(cb: (connection: FN.WS.Connection, error: any) => any): void { | ||
static onError(cb: (connection: FN.WS.Connection, error: any, ...args) => any): void { | ||
this.registerHandler(this.ON_ERROR, cb); | ||
} | ||
|
||
static async handleNewConnection(connection: FN.WS.Connection): Promise<void> { | ||
await this.getProtocol(this.ON_NEW_CONNECTION).handler(connection); | ||
} | ||
|
||
static async handleConnectionClosed(connection: FN.WS.Connection, code?: number, message?: any): Promise<void> { | ||
await this.getProtocol(this.ON_CONNECTION_CLOSED).handler(connection, code, message); | ||
static async handleNewConnection(connection: FN.WS.Connection, ...args): Promise<void> { | ||
await this.handle(this.ON_NEW_CONNECTION, connection, ...args); | ||
} | ||
|
||
static async handleMessage(connection: FN.WS.Connection, message: any): Promise<void> { | ||
await this.getProtocol(this.ON_MESSAGE).handler(connection, message); | ||
static async handleConnectionClosed(connection: FN.WS.Connection, code?: number, message?: any, ...args): Promise<void> { | ||
await this.handle(this.ON_CONNECTION_CLOSED, connection, code, message, ...args); | ||
} | ||
|
||
static async handleError(connection: FN.WS.Connection, error: any): Promise<void> { | ||
await this.getProtocol(this.ON_ERROR).handler(connection, error); | ||
static async handleMessage(connection: FN.WS.Connection, message: any, ...args): Promise<void> { | ||
await this.handle(this.ON_MESSAGE, connection, message, ...args); | ||
} | ||
|
||
static getProtocol(protocol: string): { handler: Function; } { | ||
return this.protocols.get(protocol); | ||
static async handleError(connection: FN.WS.Connection, error: any, ...args): Promise<void> { | ||
await this.handle(this.ON_ERROR, connection, error, ...args); | ||
} | ||
} |