WebSocket.ts 997 B

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