Перед вами упрощённый фрагмент клиента, который обменивается 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;
// ... копируем в Uint8Array и отправляем
}
open() {
if (this.socket) throw new Error('Already opened');
// ...
}