index.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { defineStore } from 'pinia';
  2. import { WebRTCClass } from '@/utils/network/webRTC';
  3. import { WebSocketClass } from '@/utils/network/webSocket';
  4. type NetworkRootState = {
  5. wsMap: Map<string, WebSocketClass>;
  6. rtcMap: Map<string, WebRTCClass>;
  7. };
  8. export const useNetworkStore = defineStore('network', {
  9. state: (): NetworkRootState => {
  10. return {
  11. wsMap: new Map(),
  12. rtcMap: new Map(),
  13. };
  14. },
  15. actions: {
  16. updateWsMap(roomId: string, arg) {
  17. const val = this.wsMap.get(roomId);
  18. if (val) {
  19. this.wsMap.set(roomId, { ...val, ...arg });
  20. } else {
  21. this.wsMap.set(roomId, arg);
  22. }
  23. },
  24. removeWs(roomId: string) {
  25. const old = this.wsMap.get(roomId);
  26. if (old) {
  27. old.close();
  28. }
  29. this.wsMap.delete(roomId);
  30. },
  31. updateRtcMap(socketId: string, arg) {
  32. const val = this.rtcMap.get(socketId);
  33. if (val) {
  34. this.rtcMap.set(socketId, { ...val, ...arg });
  35. } else {
  36. this.rtcMap.set(socketId, arg);
  37. }
  38. },
  39. removeRtc(socketId: string) {
  40. const old = this.rtcMap.get(socketId);
  41. if (old) {
  42. old.close();
  43. }
  44. this.rtcMap.delete(socketId);
  45. },
  46. },
  47. });