abstract websocket to allow additional transport layers

This commit is contained in:
Elijah R
2024-05-26 23:19:55 -04:00
parent 173ee8149f
commit 7053973205
7 changed files with 243 additions and 145 deletions

View File

@@ -1,14 +1,14 @@
import * as Utilities from './Utilities.js';
import * as guacutils from './guacutils.js';
import { WebSocket } from 'ws';
import { IPData } from './IPData.js';
import IConfig from './IConfig.js';
import RateLimiter from './RateLimiter.js';
import { execa, execaCommand, ExecaSyncError } from 'execa';
import { Logger } from '@cvmts/shared';
import NetworkClient from './NetworkClient.js';
export class User {
socket: WebSocket;
socket: NetworkClient;
nopSendInterval: NodeJS.Timeout;
msgRecieveInterval: NodeJS.Timeout;
nopRecieveTimeout?: NodeJS.Timeout;
@@ -28,17 +28,18 @@ export class User {
private logger = new Logger('CVMTS.User');
constructor(ws: WebSocket, ip: IPData, config: IConfig, username?: string, node?: string) {
constructor(socket: NetworkClient, ip: IPData, config: IConfig, username?: string, node?: string) {
this.IP = ip;
this.connectedToNode = false;
this.viewMode = -1;
this.Config = config;
this.socket = ws;
this.socket = socket;
this.msgsSent = 0;
this.socket.on('close', () => {
this.socket.on('disconnect', () => {
clearInterval(this.nopSendInterval);
clearInterval(this.msgRecieveInterval);
});
this.socket.on('message', (e) => {
this.socket.on('msg', (e) => {
clearTimeout(this.nopRecieveTimeout);
clearInterval(this.msgRecieveInterval);
this.msgRecieveInterval = setInterval(() => this.onNoMsg(), 10000);
@@ -73,8 +74,8 @@ export class User {
this.socket.send('3.nop;');
}
sendMsg(msg: string | Buffer) {
if (this.socket.readyState !== this.socket.OPEN) return;
sendMsg(msg: string) {
if (!this.socket.isOpen()) return;
clearInterval(this.nopSendInterval);
this.nopSendInterval = setInterval(() => this.sendNop(), 5000);
this.socket.send(msg);