export class AdvancedWebSocket
{
    constructor (
        private websocket: WebSocket,
        private timeout: NodeJS.Timeout
    ) {}

    static open(url: string, onMessage: (event: MessageEvent) => void, onClose: (event: CloseEvent) => void)
    {
        const websocket = new WebSocket(url);
        websocket.addEventListener('message', onMessage);
        websocket.addEventListener('close', onClose);
        const timeout = setInterval(() => {
            websocket.send("[PING]");
        }, 5000);
        return new AdvancedWebSocket(websocket, timeout);
    }

    close()
    {
        this.websocket.close();
        clearTimeout(this.timeout);
    }
}