-
Notifications
You must be signed in to change notification settings - Fork 13
/
websocketServerManager.ts
64 lines (50 loc) · 2.74 KB
/
websocketServerManager.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
53
54
55
56
57
58
59
60
61
62
63
64
// 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 { WebSocketServerUtil } from "../../utils/components/websocketServer.ts";
import { ClassType } from "../../utils/utilTypes.ts";
export class WebSocketServerItem {
constructor(public currentWebsocketComponent: ComponentComponent) {
}
public reStart() {
if(this.currentWebsocketComponent.internalExists(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET)) {
this.currentWebsocketComponent.getInternal<Worker>(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET).postMessage({
cmd: "RESTART",
data: {
port: this.currentWebsocketComponent.configuration.port
}
});
} else {
throw new Error("WebSocket cannot be re-started because its worker does not exist. Please mount the worker.");
}
}
public shutdown() {
if(this.currentWebsocketComponent.internalExists(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET)) {
WebSocketServerUtil.unmount(this.currentWebsocketComponent);
}
}
public mount() {
if(this.currentWebsocketComponent.internalExists(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET)) {
throw new Error("In order to mount a WebSocket server, space for worker needs to be freed. Please terminate the worker first.");
} else {
WebSocketServerUtil.mount(this.currentWebsocketComponent, this.currentWebsocketComponent.configuration.port);
}
}
public getWorkerInstance(): Worker | undefined {
return this.currentWebsocketComponent.getInternal<Worker>(MandarineConstants.COMPONENT_PROPERTY_WEBSOCKET);
}
}
export class WebSocketServerManager {
public getWebSocketClient(component: ClassType): WebSocketServerItem | 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 server item since it is not a WebSocket component");
}
return new WebSocketServerItem(componentInstance);
}
}
}