mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
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:
parent
19333c53f6
commit
e9ba195d7d
52 changed files with 815 additions and 757 deletions
49
webclient/src/websocket/events/RoomEvents.tsx
Normal file
49
webclient/src/websocket/events/RoomEvents.tsx
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { Game, Message, User } from 'types';
|
||||
import { RoomPersistence } from '../persistence/RoomPersistence';
|
||||
import { ProtobufEvents } from '../services/ProtobufService';
|
||||
|
||||
export const RoomEvents: ProtobufEvents = {
|
||||
".Event_JoinRoom.ext": joinRoom,
|
||||
".Event_LeaveRoom.ext": leaveRoom,
|
||||
".Event_ListGames.ext": listGames,
|
||||
".Event_RoomSay.ext": roomSay,
|
||||
};
|
||||
|
||||
function joinRoom({ userInfo }: JoinRoomData, { roomEvent }: RoomEvent) {
|
||||
const { roomId } = roomEvent;
|
||||
|
||||
RoomPersistence.userJoined(roomId, userInfo);
|
||||
}
|
||||
|
||||
function leaveRoom({ name }: LeaveRoomData, { roomEvent }: RoomEvent) {
|
||||
const { roomId } = roomEvent;
|
||||
RoomPersistence.userLeft(roomId, name);
|
||||
}
|
||||
|
||||
function listGames({ gameList }: ListGamesData, { roomEvent }: RoomEvent) {
|
||||
const { roomId } = roomEvent;
|
||||
RoomPersistence.updateGames(roomId, gameList);
|
||||
}
|
||||
|
||||
function roomSay(message: Message, { roomEvent }: RoomEvent) {
|
||||
const { roomId } = roomEvent;
|
||||
RoomPersistence.addMessage(roomId, message);
|
||||
}
|
||||
|
||||
interface RoomEvent {
|
||||
roomEvent: {
|
||||
roomId: number;
|
||||
}
|
||||
}
|
||||
|
||||
interface JoinRoomData {
|
||||
userInfo: User;
|
||||
}
|
||||
|
||||
interface LeaveRoomData {
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface ListGamesData {
|
||||
gameList: Game[];
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
export const JoinRoom = {
|
||||
id: ".Event_JoinRoom.ext",
|
||||
action: ({ userInfo }, webClient, { roomEvent }) => {
|
||||
const { roomId } = roomEvent;
|
||||
webClient.persistence.room.userJoined(roomId, userInfo);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
export const LeaveRoom = {
|
||||
id: ".Event_LeaveRoom.ext",
|
||||
action: ({ name }, webClient, { roomEvent }) => {
|
||||
const { roomId } = roomEvent;
|
||||
webClient.persistence.room.userLeft(roomId, name);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
export const ListGames = {
|
||||
id: ".Event_ListGames.ext",
|
||||
action: ({ gameList }, webClient, { roomEvent }) => {
|
||||
const { roomId } = roomEvent;
|
||||
webClient.persistence.room.updateGames(roomId, gameList);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
export const RoomSay = {
|
||||
id: ".Event_RoomSay.ext",
|
||||
action: (message, webClient, { roomEvent }) => {
|
||||
const { roomId } = roomEvent;
|
||||
webClient.persistence.room.addMessage(roomId, message);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
export { JoinRoom } from "./JoinRoom";
|
||||
export { LeaveRoom } from "./LeaveRoom";
|
||||
export { ListGames } from "./ListGames";
|
||||
export { RoomSay } from "./RoomSay";
|
||||
189
webclient/src/websocket/events/SessionEvents.tsx
Normal file
189
webclient/src/websocket/events/SessionEvents.tsx
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
import { Room, StatusEnum, User } from "types";
|
||||
|
||||
import { SessionCommands } from "../commands";
|
||||
import { RoomPersistence, SessionPersistence } from '../persistence';
|
||||
import { ProtobufEvents } from '../services/ProtobufService';
|
||||
import webClient from '../WebClient';
|
||||
|
||||
export const SessionEvents: ProtobufEvents = {
|
||||
".Event_AddToList.ext": addToList,
|
||||
".Event_ConnectionClosed.ext": connectionClosed,
|
||||
".Event_ListRooms.ext": listRooms,
|
||||
".Event_NotifyUser.ext": notifyUser,
|
||||
".Event_PlayerPropertiesChanges.ext": playerPropertiesChanges,
|
||||
".Event_RemoveFromList.ext": removeFromList,
|
||||
".Event_ServerIdentification.ext": serverIdentification,
|
||||
".Event_ServerMessage.ext": serverMessage,
|
||||
".Event_ServerShutdown.ext": serverShutdown,
|
||||
".Event_UserJoined.ext": userJoined,
|
||||
".Event_UserLeft.ext": userLeft,
|
||||
".Event_UserMessage.ext": userMessage,
|
||||
}
|
||||
|
||||
function addToList({ listName, userInfo}: AddToListData) {
|
||||
switch (listName) {
|
||||
case 'buddy': {
|
||||
SessionPersistence.addToBuddyList(userInfo);
|
||||
break;
|
||||
}
|
||||
case 'ignore': {
|
||||
SessionPersistence.addToIgnoreList(userInfo);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.log('Attempted to add to unknown list: ', listName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function connectionClosed({ reason, reasonStr }: ConnectionClosedData) {
|
||||
let message = "";
|
||||
|
||||
// @TODO (5)
|
||||
if (reasonStr) {
|
||||
message = reasonStr;
|
||||
} else {
|
||||
switch(reason) {
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.USER_LIMIT_REACHED:
|
||||
message = "The server has reached its maximum user capacity";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.TOO_MANY_CONNECTIONS:
|
||||
message = "There are too many concurrent connections from your address";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.BANNED:
|
||||
message = "You are banned";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.DEMOTED:
|
||||
message = "You were demoted";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.SERVER_SHUTDOWN:
|
||||
message = "Scheduled server shutdown";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.USERNAMEINVALID:
|
||||
message = "Invalid username";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.LOGGEDINELSEWERE:
|
||||
message = "You have been logged out due to logging in at another location";
|
||||
break;
|
||||
case webClient.protobuf.controller.Event_ConnectionClosed.CloseReason.OTHER:
|
||||
default:
|
||||
message = "Unknown reason";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
webClient.socket.updateStatus(StatusEnum.DISCONNECTED, message);
|
||||
}
|
||||
|
||||
function listRooms({ roomList }: ListRoomsData) {
|
||||
RoomPersistence.updateRooms(roomList);
|
||||
|
||||
if (webClient.options.autojoinrooms) {
|
||||
roomList.forEach(({ autoJoin, roomId }) => {
|
||||
if (autoJoin) {
|
||||
SessionCommands.joinRoom(roomId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function notifyUser(payload) {
|
||||
// console.info("Event_NotifyUser", payload);
|
||||
}
|
||||
|
||||
function playerPropertiesChanges(payload) {
|
||||
// console.info("Event_PlayerPropertiesChanges", payload);
|
||||
}
|
||||
|
||||
function removeFromList({ listName, userName }: RemoveFromListData) {
|
||||
switch (listName) {
|
||||
case 'buddy': {
|
||||
SessionPersistence.removeFromBuddyList(userName);
|
||||
break;
|
||||
}
|
||||
case 'ignore': {
|
||||
SessionPersistence.removeFromIgnoreList(userName);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.log('Attempted to remove from unknown list: ', listName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function serverIdentification(info: ServerIdentificationData) {
|
||||
const { serverName, serverVersion, protocolVersion } = info;
|
||||
|
||||
if (protocolVersion !== webClient.protocolVersion) {
|
||||
SessionCommands.disconnect();
|
||||
webClient.socket.updateStatus(StatusEnum.DISCONNECTED, "Protocol version mismatch: " + protocolVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
webClient.resetConnectionvars();
|
||||
webClient.socket.updateStatus(StatusEnum.LOGGINGIN, "Logging in...");
|
||||
SessionPersistence.updateInfo(serverName, serverVersion);
|
||||
SessionCommands.login();
|
||||
}
|
||||
|
||||
function serverMessage({ message }: ServerMessageData) {
|
||||
SessionPersistence.serverMessage(message);
|
||||
}
|
||||
|
||||
function serverShutdown(payload) {
|
||||
// console.info("Event_ServerShutdown", payload);
|
||||
}
|
||||
|
||||
function userJoined({ userInfo }: UserJoinedData) {
|
||||
SessionPersistence.userJoined(userInfo);
|
||||
}
|
||||
|
||||
function userLeft({ name }: UserLeftData) {
|
||||
SessionPersistence.userLeft(name);
|
||||
}
|
||||
|
||||
function userMessage(payload) {
|
||||
// console.info("Event_UserMessage", payload);
|
||||
}
|
||||
|
||||
interface SessionEvent {
|
||||
sessionEvent: {}
|
||||
}
|
||||
|
||||
interface AddToListData {
|
||||
listName: string;
|
||||
userInfo: User;
|
||||
}
|
||||
|
||||
interface ConnectionClosedData {
|
||||
endTime: number;
|
||||
reason: number;
|
||||
reasonStr: string;
|
||||
}
|
||||
|
||||
interface ListRoomsData {
|
||||
roomList: Room[];
|
||||
}
|
||||
|
||||
interface RemoveFromListData {
|
||||
listName: string;
|
||||
userName: string;
|
||||
}
|
||||
|
||||
interface ServerIdentificationData {
|
||||
protocolVersion: number;
|
||||
serverName: string;
|
||||
serverVersion: string;
|
||||
}
|
||||
|
||||
interface ServerMessageData {
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface UserJoinedData {
|
||||
userInfo: User;
|
||||
}
|
||||
|
||||
interface UserLeftData {
|
||||
name: string;
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
export const AddToList = {
|
||||
id: ".Event_AddToList.ext",
|
||||
action: ({ listName, userInfo}, webClient) => {
|
||||
switch (listName) {
|
||||
case 'buddy': {
|
||||
webClient.persistence.session.addToBuddyList(userInfo);
|
||||
break;
|
||||
}
|
||||
case 'ignore': {
|
||||
webClient.persistence.session.addToIgnoreList(userInfo);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
webClient.debug(() => console.log('Attempted to add to unknown list: ', listName));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
import { StatusEnum } from "types";
|
||||
|
||||
export const ConnectionClosed = {
|
||||
id: ".Event_ConnectionClosed.ext",
|
||||
action: ({ reason }, webClient) => {
|
||||
let message = "";
|
||||
|
||||
// @TODO (5)
|
||||
switch(reason) {
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.USER_LIMIT_REACHED:
|
||||
message = "The server has reached its maximum user capacity";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.TOO_MANY_CONNECTIONS:
|
||||
message = "There are too many concurrent connections from your address";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.BANNED:
|
||||
message = "You are banned";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.DEMOTED:
|
||||
message = "You were demoted";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.SERVER_SHUTDOWN:
|
||||
message = "Scheduled server shutdown";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.USERNAMEINVALID:
|
||||
message = "Invalid username";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.LOGGEDINELSEWERE:
|
||||
message = "You have been logged out due to logging in at another location";
|
||||
break;
|
||||
case webClient.pb.Event_ConnectionClosed.CloseReason.OTHER:
|
||||
default:
|
||||
message = "Unknown reason";
|
||||
break;
|
||||
}
|
||||
|
||||
webClient.updateStatus(StatusEnum.DISCONNECTED, message);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
import * as _ from "lodash";
|
||||
|
||||
export const ListRooms = {
|
||||
id: ".Event_ListRooms.ext",
|
||||
action: ({ roomList }, webClient) => {
|
||||
webClient.persistence.room.updateRooms(roomList);
|
||||
|
||||
if (webClient.options.autojoinrooms) {
|
||||
_.each(roomList, ({ autoJoin, roomId }) => {
|
||||
if (autoJoin) {
|
||||
webClient.commands.session.joinRoom(roomId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const NotifyUser = {
|
||||
id: ".Event_NotifyUser.ext",
|
||||
action: (payload) => {
|
||||
// console.info("Event_NotifyUser", payload);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const PlayerPropertiesChanges = {
|
||||
id: ".Event_PlayerPropertiesChanges.ext",
|
||||
action: (payload) => {
|
||||
// console.info("Event_PlayerPropertiesChanges", payload);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
export const RemoveFromList = {
|
||||
id: ".Event_RemoveFromList.ext",
|
||||
action: ({ listName, userName }, webClient) => {
|
||||
switch (listName) {
|
||||
case 'buddy': {
|
||||
webClient.persistence.session.removeFromBuddyList(userName);
|
||||
break;
|
||||
}
|
||||
case 'ignore': {
|
||||
webClient.persistence.session.removeFromIgnoreList(userName);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
webClient.debug(() => console.log('Attempted to remove from unknown list: ', listName));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
import { StatusEnum } from "types";
|
||||
|
||||
export const ServerIdentification = {
|
||||
id: ".Event_ServerIdentification.ext",
|
||||
action: (info, webClient, _raw) => {
|
||||
const { serverName, serverVersion, protocolVersion } = info;
|
||||
|
||||
if (protocolVersion !== webClient.protocolVersion) {
|
||||
webClient.disconnect();
|
||||
webClient.updateStatus(StatusEnum.DISCONNECTED, "Protocol version mismatch: " + protocolVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
webClient.resetConnectionvars();
|
||||
webClient.updateStatus(StatusEnum.LOGGINGIN, "Logging in...");
|
||||
webClient.persistence.session.updateInfo(serverName, serverVersion);
|
||||
webClient.commands.session.login();
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const ServerMessage = {
|
||||
id: ".Event_ServerMessage.ext",
|
||||
action: ({ message }, webClient) => {
|
||||
webClient.persistence.session.serverMessage(message);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const ServerShutdown = {
|
||||
id: ".Event_ServerShutdown.ext",
|
||||
action: (payload, webClient) => {
|
||||
// console.info("Event_ServerShutdown", payload);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const UserJoined = {
|
||||
id: ".Event_UserJoined.ext",
|
||||
action: ({ userInfo }, webClient) => {
|
||||
webClient.persistence.session.userJoined(userInfo);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const UserLeft = {
|
||||
id: ".Event_UserLeft.ext",
|
||||
action: ({ name }, webClient) => {
|
||||
webClient.persistence.session.userLeft(name);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export const UserMessage = {
|
||||
id: ".Event_UserMessage.ext",
|
||||
action: (payload) => {
|
||||
// console.info("Event_UserMessage", payload);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
export * from "./ConnectionClosed";
|
||||
export * from "./ListRooms";
|
||||
export * from "./AddToList";
|
||||
export * from "./RemoveFromList";
|
||||
export * from "./NotifyUser"; // @TODO
|
||||
export * from "./PlayerPropertiesChanges"; // @TODO
|
||||
export * from "./ServerIdentification";
|
||||
export * from "./ServerMessage";
|
||||
export * from "./ServerShutdown"; // @TODO
|
||||
export * from "./UserJoined";
|
||||
export * from "./UserLeft";
|
||||
export * from "./UserMessage"; // @TODO
|
||||
2
webclient/src/websocket/events/index.ts
Normal file
2
webclient/src/websocket/events/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from './RoomEvents';
|
||||
export * from './SessionEvents';
|
||||
Loading…
Add table
Add a link
Reference in a new issue