Files
Cockatrice/webclient/src/websocket/WebClient.ts
T
2a54e9d7d1 Webatrice: fix saved password (#4563)
* fix saved label, and fix using hashedPassword when Save is unchecked

* update host only after successful login

* cleanup

* fix ability to deselect saved password on successful login

* cleanup

* clear options after connection

* fix registration saved username

* cleanup

* change label

* fix tests

Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
2022-02-27 10:12:38 -06:00

93 lines
2.2 KiB
TypeScript

import { ServerStatus, StatusEnum, WebSocketConnectOptions } from 'types';
import { ProtobufService } from './services/ProtobufService';
import { WebSocketService } 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 = {
clientid: 'webatrice',
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 clientOptions = {
autojoinrooms: true,
keepalive: 5000
};
public options: WebSocketConnectOptions;
public connectionAttemptMade = false;
constructor() {
this.socket.message$.subscribe((message: MessageEvent) => {
this.protobuf.handleMessageEvent(message);
});
this.socket.statusChange$.subscribe((status: ServerStatus) => {
this.handleStatusChange(status);
});
if (process.env.NODE_ENV !== 'test') {
console.log(this);
}
}
public connect(options: WebSocketConnectOptions) {
this.connectionAttemptMade = true;
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.protobuf.resetCommands();
this.clearStores();
}
}
public keepAlive(pingReceived: Function) {
this.protobuf.sendKeepAliveCommand(pingReceived);
}
private clearStores() {
RoomPersistence.clearStore();
SessionPersistence.clearStore();
}
}
const webClient = new WebClient();
export default webClient;