roomControl.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { useRtcStore } from '/@/store/modules/rtc';
  2. import consola from 'consola';
  3. import type { ConsolaLogObject } from 'consola';
  4. import { handleActions, handleSync, handleReceivePaint, handleJoin } from './controls';
  5. // 所有socket业务事件集中点
  6. export function initSocketEvent(socket: SocketIOClient.Socket): void {
  7. if (socket) {
  8. socket.on('connect', () => {
  9. const rtcStore = useRtcStore();
  10. const params = {
  11. userId: rtcStore.userId,
  12. roomId: rtcStore.roomId,
  13. role: rtcStore.role || 'leader',
  14. avatar: rtcStore.avatar,
  15. nickname: rtcStore.nickname,
  16. };
  17. socket.emit('join', params);
  18. rtcStore.setIsJoined(true);
  19. });
  20. socket.on('action', (data: any) => {
  21. const actionLog: ConsolaLogObject = {
  22. message: data,
  23. tag: `action-${data.type || ''}`,
  24. level: 3,
  25. };
  26. consola.info(actionLog);
  27. handleActions(data);
  28. });
  29. // 自已进入逻辑
  30. socket.on('join', handleJoin);
  31. // 同屏帶看
  32. socket.on('sync', handleSync);
  33. // 畫筆
  34. socket.on('paint', handleReceivePaint);
  35. socket.on('onAny', (event: any) => {
  36. console.error('onAny:-->', event);
  37. });
  38. socket.on('error', (error: any) => {
  39. const actionLog: ConsolaLogObject = {
  40. message: error,
  41. tag: 'socket',
  42. level: 0,
  43. };
  44. consola.error(actionLog);
  45. });
  46. } else {
  47. throw new Error('socket没有初始化!');
  48. }
  49. }