websocket.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Socket, io } from 'socket.io-client';
  2. import { useNetworkStore } from '@/store/network';
  3. // websocket连接状态
  4. export enum WsConnectStatusEnum {
  5. /** 已连接 */
  6. connection = 'connection',
  7. /** 连接中 */
  8. connecting = 'connecting',
  9. /** 已连接 */
  10. connected = 'connected',
  11. /** 断开连接中 */
  12. disconnecting = 'disconnecting',
  13. /** 已断开连接 */
  14. disconnect = 'disconnect',
  15. /** 重新连接 */
  16. reconnect = 'reconnect',
  17. /** 客户端的已连接 */
  18. connect = 'connect',
  19. }
  20. // websocket消息类型
  21. export enum WsMsgTypeEnum {
  22. /** 用户进入聊天 */
  23. join = 'join',
  24. /** 用户进入聊天完成 */
  25. joined = 'joined',
  26. /** 用户进入聊天 */
  27. otherJoin = 'otherJoin',
  28. /** 用户退出聊天 */
  29. leave = 'leave',
  30. /** 用户退出聊天完成 */
  31. leaved = 'leaved',
  32. /** 当前所有在线用户 */
  33. liveUser = 'liveUser',
  34. /** 人满了 */
  35. full = 'full',
  36. /** 用户发送消息 */
  37. message = 'message',
  38. /** 管理员在线 */
  39. adminIn = 'adminIn',
  40. offer = 'offer',
  41. answer = 'answer',
  42. candidate = 'candidate',
  43. }
  44. export class WebSocketClass {
  45. socketIo: Socket | null = null;
  46. status: WsConnectStatusEnum = WsConnectStatusEnum.disconnect;
  47. url = '';
  48. roomId = '-1';
  49. isAdmin = false;
  50. constructor({ roomId, url, isAdmin }) {
  51. if (!window.WebSocket) {
  52. alert('当前环境不支持WebSocket!');
  53. return;
  54. }
  55. this.roomId = roomId;
  56. this.isAdmin = isAdmin;
  57. this.url = url;
  58. this.socketIo = io(url, { transports: ['websocket'] });
  59. this.update();
  60. // // websocket连接成功
  61. // this.socketIo.on(WsConnectStatusEnum.connect, (socket) => {
  62. // console.log('websocket连接成功', socket);
  63. // this.status = WsStatusEnum.connect;
  64. // this.update();
  65. // this.socketIo?.emit(WsMsgTypeEnum.join, { roomId: this.roomId });
  66. // });
  67. // // websocket连接断开
  68. // this.socketIo.on(WsConnectStatusEnum.disconnect, () => {
  69. // console.log('websocket连接断开', this);
  70. // this.status = WsStatusEnum.disconnect;
  71. // this.update();
  72. // });
  73. // // 用户加入房间
  74. // this.socketIo.on(WsMsgTypeEnum.join, (data) => {
  75. // console.log('用户加入房间', data);
  76. // });
  77. // // 其他用户加入房间
  78. // this.socketIo.on(WsMsgTypeEnum.otherJoin, (data) => {
  79. // console.log('其他用户加入房间', data);
  80. // });
  81. // // 用户离开房间
  82. // this.socketIo.on(WsMsgTypeEnum.leave, (data) => {
  83. // console.log('用户离开房间', data);
  84. // });
  85. // // 用户发送消息
  86. // this.socketIo.on(WsMsgTypeEnum.message, (data) => {
  87. // console.log('用户发送消息', data);
  88. // });
  89. // // 用户发送 offer
  90. // this.socketIo.on(WsMsgTypeEnum.offer, (data) => {
  91. // console.log('用户发送 offer', data);
  92. // });
  93. // // 用户发送 answer
  94. // this.socketIo.on(WsMsgTypeEnum.answer, (data) => {
  95. // console.log('用户发送 answer', data);
  96. // });
  97. }
  98. // 发送websocket消息
  99. send = ({ msgType, data }: { msgType: WsMsgTypeEnum; data?: any }) => {
  100. console.log('【websocket】发送websocket消息', msgType, data);
  101. this.socketIo?.emit(msgType, {
  102. roomId: this.roomId,
  103. socketId: this.socketIo.id,
  104. data,
  105. isAdmin: this.isAdmin,
  106. });
  107. };
  108. // 更新store
  109. update = () => {
  110. const networkStore = useNetworkStore();
  111. networkStore.updateWsMap(this.roomId, this);
  112. };
  113. // 手动关闭websocket连接
  114. close = () => {
  115. console.warn('手动关闭websocket连接', this.socketIo?.id);
  116. this.socketIo?.close();
  117. };
  118. // 连接websocket
  119. connect = () => {};
  120. }