Refactor websocket into separate services, clean up socket status communication (#4433)

* Refactor websocket into separate services, clean up socket status communication

* cleanup

* add EOF lines

* fix keepalive logged in check

* undo change

* fix keepalive connection check

* cleanup

* add typings

* secure connection

Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
Jeremy Letto 2021-10-17 00:07:30 -05:00 committed by GitHub
parent 19333c53f6
commit e9ba195d7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 815 additions and 757 deletions

View file

@ -1,44 +0,0 @@
export default class NormalizeService {
// Flatten room gameTypes into map object
static normalizeRoomInfo(roomInfo) {
roomInfo.gametypeMap = {};
const { gametypeList, gametypeMap, gameList } = roomInfo;
gametypeList.reduce((map, type) => {
map[type.gameTypeId] = type.description;
return map;
}, gametypeMap);
gameList.forEach((game) => NormalizeService.normalizeGameObject(game, gametypeMap));
}
// Flatten gameTypes[] into gameType field
// Default sortable values ("" || 0 || -1)
static normalizeGameObject(game, gametypeMap) {
const { gameTypes, description } = game;
const hasType = gameTypes && gameTypes.length;
game.gameType = hasType ? gametypeMap[gameTypes[0]] : "";
game.description = description || "";
}
// Flatten logs[] into object mapped by targetType (room, game, chat)
static normalizeLogs(logs) {
return logs.reduce((obj, log) => {
const { targetType } = log;
obj[targetType] = obj[targetType] || [];
obj[targetType].push(log);
return obj;
}, {});
}
// messages sent by current user dont have their username prepended
static normalizeUserMessage(message) {
const { name } = message;
if (name) {
message.message = `${name}: ${message.message}`;
}
}
}

View file

@ -1,33 +1,26 @@
import { store, RoomsDispatch, RoomsSelectors } from "store";
import { WebClient } from "../WebClient";
import { Game, Message, Room, User } from 'types';
import NormalizeService from "../utils/NormalizeService";
import { NormalizeService } from "websocket";
export default class RoomService {
webClient: WebClient;
constructor(webClient) {
this.webClient = webClient;
}
clearStore() {
export class RoomPersistence {
static clearStore() {
RoomsDispatch.clearStore();
}
joinRoom(roomInfo) {
static joinRoom(roomInfo: Room) {
NormalizeService.normalizeRoomInfo(roomInfo);
RoomsDispatch.joinRoom(roomInfo);
}
leaveRoom(roomId) {
static leaveRoom(roomId: number) {
RoomsDispatch.leaveRoom(roomId);
}
updateRooms(rooms) {
static updateRooms(rooms: Room[]) {
RoomsDispatch.updateRooms(rooms);
}
updateGames(roomId, gameList) {
static updateGames(roomId: number, gameList: Game[]) {
const game = gameList[0];
if (!game.gameType) {
@ -42,17 +35,17 @@ export default class RoomService {
RoomsDispatch.updateGames(roomId, gameList);
}
addMessage(roomId, message) {
static addMessage(roomId: number, message: Message) {
NormalizeService.normalizeUserMessage(message);
RoomsDispatch.addMessage(roomId, message);
}
userJoined(roomId, user) {
static userJoined(roomId: number, user: User) {
RoomsDispatch.userJoined(roomId, user);
}
userLeft(roomId, name) {
static userLeft(roomId: number, name: string) {
RoomsDispatch.userLeft(roomId, name);
}
}

View file

@ -0,0 +1,75 @@
import { ServerDispatch } from "store";
import { Log, StatusEnum, User } from "types";
import { sanitizeHtml } from "websocket/utils";
import NormalizeService from "../utils/NormalizeService";
export class SessionPersistence {
static clearStore() {
ServerDispatch.clearStore();
}
static connectionClosed(reason: number) {
ServerDispatch.connectionClosed(reason);
}
static updateBuddyList(buddyList) {
ServerDispatch.updateBuddyList(buddyList);
}
static addToBuddyList(user: User) {
ServerDispatch.addToBuddyList(user);
}
static removeFromBuddyList(userName: string) {
ServerDispatch.removeFromBuddyList(userName);
}
static updateIgnoreList(ignoreList) {
ServerDispatch.updateIgnoreList(ignoreList);
}
static addToIgnoreList(user: User) {
ServerDispatch.addToIgnoreList(user);
}
static removeFromIgnoreList(userName: string) {
ServerDispatch.removeFromIgnoreList(userName);
}
static updateInfo(name: string, version: string) {
ServerDispatch.updateInfo(name, version);
}
static updateStatus(state: number, description: string) {
ServerDispatch.updateStatus(state, description);
if (state === StatusEnum.DISCONNECTED) {
this.connectionClosed(state);
}
}
static updateUser(user: User) {
ServerDispatch.updateUser(user);
}
static updateUsers(users: User[]) {
ServerDispatch.updateUsers(users);
}
static userJoined(user: User) {
ServerDispatch.userJoined(user);
}
static userLeft(userName: string) {
ServerDispatch.userLeft(userName);
}
static viewLogs(logs: Log[]) {
ServerDispatch.viewLogs(NormalizeService.normalizeLogs(logs));
}
static serverMessage(message: string) {
ServerDispatch.serverMessage(sanitizeHtml(message));
}
}

View file

@ -1,94 +0,0 @@
import { ServerDispatch, ServerConnectParams } from "store";
import { StatusEnum } from "types";
import { sanitizeHtml } from "websocket/utils";
import { WebClient } from "websocket/WebClient";
import { NormalizeService } from "websocket";
export default class SessionService {
webClient: WebClient;
constructor(webClient) {
this.webClient = webClient;
}
clearStore() {
ServerDispatch.clearStore();
}
connectServer(options: ServerConnectParams) {
ServerDispatch.connectServer();
this.webClient.updateStatus(StatusEnum.CONNECTING, "Connecting...");
this.webClient.connect(options);
}
disconnectServer() {
this.webClient.updateStatus(StatusEnum.DISCONNECTING, "Disconnecting...");
this.webClient.disconnect();
}
connectionClosed(reason) {
ServerDispatch.connectionClosed(reason);
}
updateBuddyList(buddyList) {
ServerDispatch.updateBuddyList(buddyList);
}
addToBuddyList(user) {
ServerDispatch.addToBuddyList(user);
}
removeFromBuddyList(userName) {
ServerDispatch.removeFromBuddyList(userName);
}
updateIgnoreList(ignoreList) {
ServerDispatch.updateIgnoreList(ignoreList);
}
addToIgnoreList(user) {
ServerDispatch.addToIgnoreList(user);
}
removeFromIgnoreList(userName) {
ServerDispatch.removeFromIgnoreList(userName);
}
updateInfo(name, version) {
ServerDispatch.updateInfo(name, version);
}
updateStatus(state, description) {
ServerDispatch.updateStatus(state, description);
if (state === StatusEnum.DISCONNECTED) {
this.connectionClosed({ reason: description });
}
}
updateUser(user) {
ServerDispatch.updateUser(user);
}
updateUsers(users) {
ServerDispatch.updateUsers(users);
}
userJoined(user) {
ServerDispatch.userJoined(user);
}
userLeft(userId) {
ServerDispatch.userLeft(userId);
}
viewLogs(logs) {
ServerDispatch.viewLogs(NormalizeService.normalizeLogs(logs));
}
serverMessage(message) {
ServerDispatch.serverMessage(sanitizeHtml(message));
}
}

View file

@ -1,3 +1,2 @@
export { default as NormalizeService } from "./NormalizeService";
export { default as RoomService } from "./RoomService";
export { default as SessionService } from "./SessionService";
export { RoomPersistence } from "./RoomPersistence";
export { SessionPersistence } from "./SessionPersistence";