TeaWeb/shared/js/connection/ConnectionBase.ts

129 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-03-30 11:44:18 +00:00
import {CommandHelper} from "tc-shared/connection/CommandHelper";
import {HandshakeHandler} from "tc-shared/connection/HandshakeHandler";
import {CommandResult} from "tc-shared/connection/ServerConnectionDeclaration";
import {ServerAddress} from "tc-shared/ui/server";
import {RecorderProfile} from "tc-shared/voice/RecorderProfile";
import {ConnectionHandler, ConnectionState} from "tc-shared/ConnectionHandler";
import {AbstractCommandHandlerBoss} from "tc-shared/connection/AbstractCommandHandler";
export interface CommandOptions {
flagset?: string[]; /* default: [] */
process_result?: boolean; /* default: true */
timeout?: number /* default: 1000 */;
}
export const CommandOptionDefaults: CommandOptions = {
flagset: [],
process_result: true,
timeout: 1000
};
export type ConnectionStateListener = (old_state: ConnectionState, new_state: ConnectionState) => any;
export abstract class AbstractServerConnection {
readonly client: ConnectionHandler;
readonly command_helper: CommandHelper;
protected constructor(client: ConnectionHandler) {
this.client = client;
this.command_helper = new CommandHelper(this);
2019-02-23 13:15:22 +00:00
}
2020-03-30 11:44:18 +00:00
/* resolved as soon a connection has been established. This does not means that the authentication had yet been done! */
abstract connect(address: ServerAddress, handshake: HandshakeHandler, timeout?: number) : Promise<void>;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
abstract connected() : boolean;
abstract disconnect(reason?: string) : Promise<void>;
2019-04-04 19:47:52 +00:00
2020-03-30 11:44:18 +00:00
abstract support_voice() : boolean;
abstract voice_connection() : voice.AbstractVoiceConnection | undefined;
2019-04-15 13:33:51 +00:00
2020-03-30 11:44:18 +00:00
abstract command_handler_boss() : AbstractCommandHandlerBoss;
abstract send_command(command: string, data?: any | any[], options?: CommandOptions) : Promise<CommandResult>;
2019-08-21 08:00:01 +00:00
2020-03-30 11:44:18 +00:00
abstract get onconnectionstatechanged() : ConnectionStateListener;
abstract set onconnectionstatechanged(listener: ConnectionStateListener);
2020-03-30 11:44:18 +00:00
abstract remote_address() : ServerAddress; /* only valid when connected */
abstract handshake_handler() : HandshakeHandler; /* only valid when connected */
2020-03-30 11:44:18 +00:00
abstract ping() : {
native: number,
javascript?: number
};
}
export namespace voice {
export enum PlayerState {
PREBUFFERING,
PLAYING,
BUFFERING,
STOPPING,
STOPPED
2019-02-23 13:15:22 +00:00
}
2020-03-30 11:44:18 +00:00
export type LatencySettings = {
min_buffer: number; /* milliseconds */
max_buffer: number; /* milliseconds */
2019-02-23 13:15:22 +00:00
}
2020-03-30 11:44:18 +00:00
export interface VoiceClient {
client_id: number;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
callback_playback: () => any;
callback_stopped: () => any;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
callback_state_changed: (new_state: PlayerState) => any;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
get_state() : PlayerState;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
get_volume() : number;
set_volume(volume: number) : void;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
abort_replay();
support_latency_settings() : boolean;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
reset_latency_settings();
latency_settings(settings?: LatencySettings) : LatencySettings;
2020-03-30 11:44:18 +00:00
support_flush() : boolean;
flush();
2020-03-27 22:36:57 +00:00
}
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
export abstract class AbstractVoiceConnection {
readonly connection: AbstractServerConnection;
2019-08-21 08:00:01 +00:00
protected constructor(connection: AbstractServerConnection) {
this.connection = connection;
}
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
abstract connected() : boolean;
abstract encoding_supported(codec: number) : boolean;
abstract decoding_supported(codec: number) : boolean;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
abstract register_client(client_id: number) : VoiceClient;
abstract available_clients() : VoiceClient[];
abstract unregister_client(client: VoiceClient) : Promise<void>;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
abstract voice_recorder() : RecorderProfile;
abstract acquire_voice_recorder(recorder: RecorderProfile | undefined) : Promise<void>;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
abstract get_encoder_codec() : number;
abstract set_encoder_codec(codec: number);
}
}
2020-03-30 11:44:18 +00:00
export class ServerCommand {
command: string;
arguments: any[];
}
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
export interface SingleCommandHandler {
name?: string;
command?: string;
timeout?: number;
2019-02-23 13:15:22 +00:00
2020-03-30 11:44:18 +00:00
/* if the return is true then the command handler will be removed */
function: (command: ServerCommand) => boolean;
2019-02-23 13:15:22 +00:00
}