"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Observer = void 0;
const socket_io_client_1 = __importDefault(require("socket.io-client"));
class Observer {
    constructor(serverData, g, reconnect = false) {
        this.serverRegion = serverData.region;
        this.serverIdentifier = serverData.name;
        this.G = g;
        this.socket = socket_io_client_1.default(`ws://${serverData.addr}:${serverData.port}`, {
            autoConnect: false,
            reconnection: reconnect,
            transports: ["websocket"]
        });
    }
    async connect() {
        console.debug(`Connecting to ${this.serverRegion}${this.serverIdentifier}...`);
        const connected = new Promise((resolve, reject) => {
            this.socket.on("welcome", (data) => {
                if (data.region !== this.serverRegion || data.name !== this.serverIdentifier) {
                    reject(`We wanted the server ${this.serverRegion}${this.serverIdentifier}, but we are on ${data.region}${data.name}.`);
                }
                else {
                    this.socket.emit("loaded", {
                        height: 1080,
                        width: 1920,
                        scale: 2,
                        success: 1
                    });
                    resolve();
                }
            });
            setTimeout(() => {
                reject("Failed to start within 10s.");
            }, 10000);
        });
        this.socket.open();
        return connected;
    }
}
exports.Observer = Observer;
//# sourceMappingURL=Observer.js.map