64 lines
1.3 KiB
TypeScript
64 lines
1.3 KiB
TypeScript
// src/lib/socket-session.ts
|
|
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
|
|
// All Rights Reserved
|
|
|
|
import { Socket } from "socket.io";
|
|
import {
|
|
ClientToServerEvents,
|
|
IUser,
|
|
ServerToClientEvents,
|
|
SocketData,
|
|
} from "@gadget/api";
|
|
import { DtpLog } from "./log.js";
|
|
|
|
export enum SocketSessionType {
|
|
Code = "code",
|
|
Drone = "drone",
|
|
}
|
|
|
|
export type GadgetSocket = Socket<
|
|
ClientToServerEvents,
|
|
ServerToClientEvents,
|
|
never,
|
|
SocketData
|
|
>;
|
|
export abstract class SocketSession {
|
|
protected log: DtpLog;
|
|
protected _socket: GadgetSocket;
|
|
protected _user: IUser;
|
|
|
|
public get socket() {
|
|
return this._socket;
|
|
}
|
|
|
|
public get user() {
|
|
return this._user;
|
|
}
|
|
|
|
protected abstract type: SocketSessionType;
|
|
|
|
constructor(socket: GadgetSocket, user: IUser) {
|
|
this.log = new DtpLog({
|
|
name: "SocketSession",
|
|
slug: "lib:socket-session",
|
|
});
|
|
this._socket = socket;
|
|
this._user = user;
|
|
}
|
|
|
|
/**
|
|
* Registers socket event and message handlers intended to be processed by all
|
|
* socket sessions of all types.
|
|
*/
|
|
register() {
|
|
this.socket.on("disconnect", this.onSocketDisconnect.bind(this));
|
|
}
|
|
|
|
async onSocketDisconnect(): Promise<void> {
|
|
this.log.info("socket disconnected", {
|
|
id: this.socket.id,
|
|
userId: this.user._id,
|
|
});
|
|
}
|
|
}
|