Этот код пришёл к вам на ревью. Какие проблемы вы видите и что из этого может сломаться в продакшене? Начните с самого опасного.
Клиент игры обменивается с сервером JSON-сообщениями через WebSocket. Перед каждым сообщением в бинарном кадре идут два байта с его длиной. Ниже упрощённый код приёма, отправки и открытия соединения:
socket.onmessage = (e) => {
const arr = new Uint8Array(e.data);
const data = String.fromCharCode.apply(null, arr as any);
// we just crop two-byte length, because WebSocket protocol make package splitting
const json = JSON.parse(decodeURIComponent(escape(data.substr(2))));
this.onrecv(json);
};
send(obj) {
const s = unescape(encodeURIComponent(JSON.stringify(obj)));
const str = String.fromCharCode(Math.floor(s.length / 256), s.length % 256) + s;
// ...копируем str в Uint8Array и вызываем this.socket.send(...)
}
open() {
if (this.socket) throw new Error('Already opened');
this.socket = new WebSocket(this.url);
// ...навешиваем обработчики
}