Skip to content

Commit

Permalink
simplify get method
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharbonnier committed Nov 13, 2019
1 parent 9c3a525 commit 2d2a9b8
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ export class Router {

public get(id: string): WebSocket {
if (!this.virtualWebSockets.has(id)) {
const routedWs = new RoutedWebSocket(
(data: string | ArrayBufferLike | Blob | ArrayBufferView) => this.send(id, data),
(code: number, reason: string) => this.close(id, code, reason),
(vWs) => this.onMessageSubscribe(id, vWs),
(vWs) => this.onMessageUnsubscribe(id, vWs),
);
const routedWs = this.createRoutedWebsocket(id);
this.virtualWebSockets.set(id, routedWs);
if (this.localWebSockets.has(id)) {
routedWs.setReadyState(this.localWebSockets.get(id).readyState);
Expand Down Expand Up @@ -168,6 +163,14 @@ export class Router {
}
}

private createRoutedWebsocket(id: string): RoutedWebSocket {
return new RoutedWebSocket(
(data: string | ArrayBufferLike | Blob | ArrayBufferView) => this.send(id, data),
(code: number, reason: string) => this.close(id, code, reason),
(vWs) => this.onMessageSubscribe(id, vWs),
(vWs) => this.onMessageUnsubscribe(id, vWs),
);
}
private send(id: string, data: string | ArrayBufferLike | Blob | ArrayBufferView) {
const ws = this.localWebSockets.get(id);
if (ws && ws.readyState === WebSocket.OPEN) {
Expand Down

0 comments on commit 2d2a9b8

Please sign in to comment.