That's a spicy meatball
* APIUtils additions * Socket and web server listen on dedicated ports (see denoland/deno socket issue created by ZombieB1309 on GitHub) * Coach and Server created automatically (untested) * Profile content functions split into 'managers' * Progression temporary implementation * Settings placed into profile content manager * Relationships and messages return temporary empty array * Socket targets defined, message delivery to target, exec returned (goes unused for now)
This commit is contained in:
@@ -1,43 +1,77 @@
|
||||
import WebSocket from "ws";
|
||||
import { Profile } from "../data/profiles.ts";
|
||||
import Logging from "@proxnet/undead-logging";
|
||||
import { Message, MessageKind, SignalMessageType, SignalRMessage, SignalRMessageSchema, TargetResult, TargetResultFailure, TargetResultSuccess, TargetResultType } from "./types.ts";
|
||||
import { SocketTarget } from "./targets/targetbase.ts";
|
||||
import { PlayerSocketSubscriptionTarget } from "./targets/SubscribeToPlayers.ts";
|
||||
|
||||
export class SignalRSocketHandler {
|
||||
|
||||
log: Logging = new Logging("SignalMock-");
|
||||
#log: Logging = new Logging("SignalMock-");
|
||||
|
||||
#socket: WebSocket;
|
||||
#profile: Profile;
|
||||
|
||||
#Targets: Map<string, SocketTarget> = new Map();
|
||||
|
||||
constructor(socket: WebSocket, player: Profile) {
|
||||
|
||||
this.#socket = socket;
|
||||
this.#initLogSource();
|
||||
|
||||
this.#profile = player;
|
||||
|
||||
this.#init();
|
||||
|
||||
player.setSocketHandler(this);
|
||||
|
||||
Deno.addSignalListener('SIGINT', this.destroy);
|
||||
this.#Targets.set('SubscribeToPlayers', new PlayerSocketSubscriptionTarget());
|
||||
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.#socket.close();
|
||||
Deno.removeSignalListener('SIGINT', this.destroy);
|
||||
async #dispatchTarget<T = unknown>(target: string, args: object[]): Promise<TargetResult> {
|
||||
const targetExec = this.#Targets.get(target);
|
||||
if (!targetExec) return { type: TargetResultType.Failure } as TargetResultFailure;
|
||||
else return { type: TargetResultType.Success, data: await targetExec.exec(args) } as TargetResultSuccess<T>;
|
||||
}
|
||||
|
||||
async #initLogSource() {
|
||||
this.log.source += this.#profile.getId().toString();
|
||||
#onMessage(message: Message) {
|
||||
if (message.kind == MessageKind.Protocol) {
|
||||
this.#send({});
|
||||
return;
|
||||
} else {
|
||||
this.#log.d(`CLIENT MESSAGE\n Type: ${message.data.type} (${SignalMessageType[message.data.type - 1]})\n ${JSON.stringify(message.data)}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.log.i(`Player '${(await this.#profile.export())?.username}' (${this.#profile.getId()}) created hub socket`);
|
||||
async #init() {
|
||||
this.#log.source += this.#profile.getId().toString();
|
||||
|
||||
this.#socket.on('open', () => {
|
||||
this.log.d(`hello world`)
|
||||
});
|
||||
this.#socket.on('message', data => {
|
||||
this.log.d(data.toString());
|
||||
this.#log.i(`Player '${(await this.#profile.export())?.username}' (${this.#profile.getId()}) created hub socket`);
|
||||
|
||||
this.#socket.addEventListener('message', message => {
|
||||
try {
|
||||
|
||||
const dec = new TextDecoder();
|
||||
const str = dec.decode(message.data);
|
||||
const data = JSON.parse(str.substring(0, str.length - 1));
|
||||
|
||||
const parseResult = SignalRMessageSchema.safeParse(data);
|
||||
if (parseResult.success) this.#onMessage({
|
||||
kind: MessageKind.Data,
|
||||
data: parseResult.data as SignalRMessage
|
||||
});
|
||||
else {
|
||||
this.#onMessage({
|
||||
kind: MessageKind.Protocol
|
||||
});
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
this.#log.e(`Socket error: ${err}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#send(data: object) {
|
||||
this.#socket.send(`${JSON.stringify(data)}\u001e`);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user