mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-01-19 00:06:01 -08:00
cleanup and unit tests (#4434)
* put socket.updateHistory behind SessionCommand * rename /websocket files from .tsx to .ts * add unit tests to websocket commands * complete unit tests for webClient commands * secure wss Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
90
webclient/src/websocket/WebClient.ts
Normal file
90
webclient/src/websocket/WebClient.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { ServerConnectParams } from "store";
|
||||
import { ServerStatus, StatusEnum } from "types";
|
||||
|
||||
import { ProtobufService } from './services/ProtobufService';
|
||||
import { WebSocketService, WebSocketOptions } from "./services/WebSocketService";
|
||||
|
||||
import { RoomPersistence, SessionPersistence } from './persistence';
|
||||
|
||||
export class WebClient {
|
||||
public socket = new WebSocketService(this);
|
||||
public protobuf = new ProtobufService(this);
|
||||
|
||||
public protocolVersion = 14;
|
||||
public clientConfig = {
|
||||
"clientver" : "webclient-1.0 (2019-10-31)",
|
||||
"clientfeatures" : [
|
||||
"client_id",
|
||||
"client_ver",
|
||||
"feature_set",
|
||||
"room_chat_history",
|
||||
"client_warnings",
|
||||
/* unimplemented features */
|
||||
"forgot_password",
|
||||
"idle_client",
|
||||
"mod_log_lookup",
|
||||
"user_ban_history",
|
||||
// satisfy server reqs for POC
|
||||
"websocket",
|
||||
"2.7.0_min_version",
|
||||
"2.8.0_min_version"
|
||||
]
|
||||
};
|
||||
|
||||
public options: WebSocketOptions = {
|
||||
host: "",
|
||||
port: "",
|
||||
user: "",
|
||||
pass: "",
|
||||
autojoinrooms: true,
|
||||
keepalive: 5000
|
||||
};
|
||||
|
||||
constructor() {
|
||||
this.socket.message$.subscribe((message: MessageEvent) => {
|
||||
this.protobuf.handleMessageEvent(message);
|
||||
});
|
||||
|
||||
this.socket.statusChange$.subscribe((status: ServerStatus) => {
|
||||
this.handleStatusChange(status);
|
||||
});
|
||||
|
||||
console.log(this);
|
||||
}
|
||||
|
||||
public connect(options: ServerConnectParams) {
|
||||
this.options = { ...this.options, ...options };
|
||||
this.socket.connect(this.options);
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this.socket.disconnect();
|
||||
}
|
||||
|
||||
public updateStatus(status: StatusEnum, description: string) {
|
||||
this.socket.updateStatus(status, description);
|
||||
}
|
||||
|
||||
public handleStatusChange({ status, description }: ServerStatus) {
|
||||
SessionPersistence.updateStatus(status, description);
|
||||
|
||||
if (status === StatusEnum.DISCONNECTED) {
|
||||
this.resetConnectionvars();
|
||||
this.clearStores();
|
||||
}
|
||||
}
|
||||
|
||||
public resetConnectionvars() {
|
||||
this.protobuf.resetCommands();
|
||||
this.socket.keepAliveService.resetPingFlag();
|
||||
}
|
||||
|
||||
private clearStores() {
|
||||
RoomPersistence.clearStore();
|
||||
SessionPersistence.clearStore();
|
||||
}
|
||||
}
|
||||
|
||||
const webClient = new WebClient();
|
||||
|
||||
export default webClient;
|
||||
Reference in New Issue
Block a user