WebSocket.ts 933 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { WebSocket as WSWebSocket } from "ws";
  2. import LogBook, { Log } from "@/LogBook";
  3. export default class WebSocket extends WSWebSocket {
  4. private _socketId?: string;
  5. private _sessionId?: string;
  6. public dispatch(name: string, ...args: any[]) {
  7. this.send(JSON.stringify([name, ...args]));
  8. }
  9. /**
  10. * log - Add log to logbook
  11. *
  12. * @param log - Log message or object
  13. */
  14. public log(log: string | Omit<Log, "timestamp" | "category">) {
  15. const {
  16. message,
  17. type = undefined,
  18. data = {}
  19. } = {
  20. ...(typeof log === "string" ? { message: log } : log)
  21. };
  22. LogBook.log({
  23. message,
  24. type,
  25. category: "modules.websocket.socket",
  26. data
  27. });
  28. }
  29. public getSocketId() {
  30. return this._socketId;
  31. }
  32. public setSocketId(socketId?: string) {
  33. this._socketId = socketId;
  34. }
  35. public getSessionId() {
  36. return this._sessionId;
  37. }
  38. public setSessionId(sessionId?: string) {
  39. this._sessionId = sessionId;
  40. }
  41. }