<commit_before>import * as express from 'express';
import * as http from 'http';
import * as WebSocket from 'ws';
import { logFactory, Logger } from "../log-config";
import { CommandComponent } from "./index";
import * as common from "akibot-common/dist";

export class WebSocketServerComponent {

    private logger: Logger = logFactory.getLogger(this.constructor.name);

    // TODO: parameter
    private port: number = 3000;

    private expressApplication: express.Application;
    private httpServer: http.Server;
    private wss: WebSocket.Server;
    private clients: WebSocket[] = [];

    constructor(private commandComponent: CommandComponent) {
        this.logger.debug("constructor");
        this.expressApplication = express();
    }

    private onMessage(client: WebSocket, data: WebSocket.Data) {
        this.logger.trace("onMessage: " + data.toString());
        var jsonInput = common.SerializationUtils.jsonParse(data.toString());
        var message: common.Message = common.SerializationUtils.deserialize(jsonInput, common);
        this.commandComponent.commandEvents.once(common.OrientationResponse.name, (orientationResponse: common.OrientationResponse) => {
            this.broadcast(orientationResponse);
        });
        this.commandComponent.emitMessage(message);
    }

    public broadcast(message: common.Message) {
        var msgText: string = common.SerializationUtils.jsonStringify(message);
        this.logger.trace("Broadcasting to " + this.clients.length + " clients: " + msgText);
        var idx = 0;
        this.clients.forEach((i) => {
            idx++;
            this.logger.trace("Send to client " + idx + ": " + msgText);
            try {
                i.send(msgText);
            } catch (e) {
                this.logger.warn("Unable to send message: " + e);
                // TODO: do something with such client
            }
        });
    }

    public start(): Promise<void> {
        this.logger.debug("Starting WSS..");
        return new Promise<void>((resolve, reject) => {
            this.httpServer = http.createServer(this.expressApplication);
            this.wss = new WebSocket.Server({ server: this.httpServer });

            this.wss.on("connection", (client: WebSocket, request: http.IncomingMessage) => {
                client.on("message", (data: WebSocket.Data) => this.onMessage(client, data));
                this.clients.push(client);
            });

            this.httpServer.listen(this.port, () => {
                this.logger.info(`Server is listening on port ${this.httpServer.address().port}`);
            });

            return resolve();
        });
    }


    public stop():void {
        this.logger.debug("Stopping WSS..");        
        this.wss.close();
        this.httpServer.close();
    }


}
<commit_msg>onClientClose<commit_after>import * as express from 'express';
import * as http from 'http';
import * as WebSocket from 'ws';
import { logFactory, Logger } from "../log-config";
import { CommandComponent } from "./index";
import * as common from "akibot-common/dist";

export class WebSocketServerComponent {

    private logger: Logger = logFactory.getLogger(this.constructor.name);

    // TODO: parameter
    private port: number = 3000;

    private expressApplication: express.Application;
    private httpServer: http.Server;
    private wss: WebSocket.Server;
    private clients: WebSocket[] = [];

    constructor(private commandComponent: CommandComponent) {
        this.logger.debug("constructor");
        this.expressApplication = express();
    }

    private onMessage(client: WebSocket, data: WebSocket.Data) {
        this.logger.trace("onMessage: " + data.toString());
        var jsonInput = common.SerializationUtils.jsonParse(data.toString());
        var message: common.Message = common.SerializationUtils.deserialize(jsonInput, common);
        this.commandComponent.commandEvents.once(common.OrientationResponse.name, (orientationResponse: common.OrientationResponse) => {
            this.broadcast(orientationResponse);
        });
        this.commandComponent.emitMessage(message);
    }

    private onClientClose(client: WebSocket, code: number, message: string) {
        this.logger.info("onClientClose: " + code + ": " + message);
        this.removeClient(client);
    }

    private removeClient(client: WebSocket) {
        this.logger.debug("removeClient");
        var index = this.clients.indexOf(client, 0);
        if (index > -1) {
            this.clients.splice(index, 1);
        }
    }

    public broadcast(message: common.Message) {
        var msgText: string = common.SerializationUtils.jsonStringify(message);
        this.logger.trace("Broadcasting to " + this.clients.length + " clients: " + msgText);
        var idx = 0;
        this.clients.forEach((i) => {
            idx++;
            this.logger.trace("Send to client " + idx + ": " + msgText);
            try {
                i.send(msgText);
            } catch (e) {
                this.logger.warn("Unable to send message: " + e);
                // TODO: do something with such client
            }
        });
    }

    public start(): Promise<void> {
        this.logger.debug("Starting WSS..");
        return new Promise<void>((resolve, reject) => {
            this.httpServer = http.createServer(this.expressApplication);
            this.wss = new WebSocket.Server({ server: this.httpServer });

            this.wss.on("connection", (client: WebSocket, request: http.IncomingMessage) => {
                this.clients.push(client);
                client.on("message", (data: WebSocket.Data) => this.onMessage(client, data));
                client.on("close", (code: number, message: string) => this.onClientClose(client, code, message));
            });

            this.httpServer.listen(this.port, () => {
                this.logger.info(`Server is listening on port ${this.httpServer.address().port}`);
            });

            return resolve();
        });
    }


    public stop(): void {
        this.logger.debug("Stopping WSS..");
        this.wss.close();
        this.httpServer.close();
    }


}

{'fraction_non_alphanumeric': 0.13237200259235257, 'fraction_numerical': 0.002106286454957874, 'mean_word_length': 2.481669486745629, 'pattern_counts': {'":': 3, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '807993', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1622, 'n_words': 351}