"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const connection_endpoint_1 = require("../../base/connection-endpoint");
const socket_wrapper_factory_1 = require("./socket-wrapper-factory");
const textMessageBuilder = require("./text-protocol/message-builder");
const constants_1 = require("../../../constants");
class WSTextConnectionEndpoint extends connection_endpoint_1.default {
    constructor(wsOptions, services, config) {
        super(wsOptions, services, config);
        this.wsOptions = wsOptions;
        this.description = 'WS Text Protocol Connection Endpoint';
        this.pingMessage = textMessageBuilder.getMessage({
            topic: constants_1.TOPIC.CONNECTION,
            action: constants_1.CONNECTION_ACTION.PING
        });
    }
    async init() {
        super.init();
        this.services.httpService.registerWebsocketEndpoint(this.wsOptions.urlPath, socket_wrapper_factory_1.createWSSocketWrapper, this);
    }
    onConnection(socketWrapper) {
        super.onConnection(socketWrapper);
        socketWrapper.onMessage = socketWrapper.authCallback;
        socketWrapper.sendMessage({
            topic: constants_1.TOPIC.CONNECTION,
            action: constants_1.CONNECTION_ACTION.ACCEPT
        }, false);
        this.sendPing(socketWrapper);
    }
    sendPing(socketWrapper) {
        if (!socketWrapper.isClosed) {
            socketWrapper.sendBuiltMessage(this.pingMessage);
            setTimeout(this.sendPing.bind(this, socketWrapper), this.wsOptions.heartbeatInterval);
        }
    }
}
exports.WSTextConnectionEndpoint = WSTextConnectionEndpoint;
//# sourceMappingURL=connection-endpoint.js.map