-
Notifications
You must be signed in to change notification settings - Fork 13
/
websocketClientManager.ts
52 lines (39 loc) · 2.34 KB
/
websocketClientManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2020-2020 The Mandarine.TS Framework authors. All rights reserved. MIT license.
import { ApplicationContext } from "../../application-context/mandarineApplicationContext.ts";
import type { ComponentComponent } from "../../components/component-component/componentComponent.ts";
import { Mandarine } from "../../Mandarine.ns.ts";
import { MandarineConstants } from "../../mandarineConstants.ts";
import { WebSocketClientUtil } from "../../utils/components/websocketClient.ts";
import { ClassType } from "../../utils/utilTypes.ts";
export class WebSocketClientItem {
constructor(public currentWebsocketComponent: ComponentComponent) {
}
public close(code?: any, reason?: any) {
if(this.currentWebsocketComponent.internalExists(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET)) {
this.currentWebsocketComponent.getInternal<WebSocket>(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET).close(code, reason);
}
}
public mount() {
if(this.currentWebsocketComponent.internalExists(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET)) {
throw new Error("In order to mount a new WebSocket instance, space for the WebSocket client needs to be freed. Please close the current client first.")
} else {
const { url, protocols } = this.currentWebsocketComponent.configuration;
WebSocketClientUtil.mount(this.currentWebsocketComponent, url, protocols);
}
}
public getWebSocketInstance(): WebSocket | undefined {
return this.currentWebsocketComponent.getInternal<WebSocket>(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET);
}
}
export class WebSocketClientManager {
public getWebSocketServer(component: ClassType): WebSocketClientItem | undefined {
const internalComponent = ApplicationContext.getInstance().getComponentsRegistry().getComponentByHandlerType(component);
if(internalComponent) {
const componentInstance: ComponentComponent = internalComponent.componentInstance;
if(internalComponent.componentType !== Mandarine.MandarineCore.ComponentTypes.WEBSOCKET) {
throw new Error("Component was found but it is not valid as a WebSocket client item since it is not a WebSocket component");
}
return new WebSocketClientItem(componentInstance);
}
}
}