Signal.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //const log$4 = new Logger("xverse-signal");
  2. import Codes from "./enum/Codes.js"
  3. import {logger} from "./Logger.js"
  4. export default class Signal {
  5. constructor(e) {
  6. this.signalHandleActived = !0
  7. this.isUpdatedYUV = !0
  8. this._room = e
  9. }
  10. handleSignal(e) {
  11. var a, s, l;
  12. if (!this.signalHandleActived)
  13. return;
  14. const {signal: t, alreadyUpdateYUV: r} = e;
  15. if (this.handleActionResponses(t),
  16. this._room.handleSignalHook(t),
  17. !r) {
  18. const u = (a = t.newUserStates) == null ? void 0 : a.find(c=>c.userId === this._room.userId);
  19. if ((u == null ? void 0 : u.renderInfo) && ((s = this._room._userAvatar) == null ? void 0 : s.isMoving)) {
  20. logger.debug("stream stoped, make avatar to stop");
  21. const {isMoving: c, isRotating: h} = u.renderInfo;
  22. this._room.avatarManager._updateAvatarMovingStatus({
  23. id: u.userId,
  24. isMoving: !!c,
  25. isRotating: !!h
  26. })
  27. }
  28. return
  29. }
  30. this.isUpdatedYUV = r;
  31. const n = t;
  32. if (!t) {
  33. logger.warn("metadata signal is empty");
  34. return
  35. }
  36. if (n.code === Codes.RepeatLogin) {
  37. this._room.handleRepetLogin();
  38. return
  39. }
  40. n.code !== void 0 && n.code !== Codes.Success && n.code !== Codes.ActionMaybeDelay && n.code !== Codes.DoActionBlocked && n.code !== Codes.GetOnVehicle && (logger.error("signal errcode: ", n),
  41. this._room.emit("error", n));
  42. const o = (l = n.newUserStates) == null ? void 0 : l.find(u=>u.userId === this._room.userId);
  43. if (n.broadcastAction)
  44. try {
  45. const u = JSON.parse(n.broadcastAction.data);
  46. Broadcast.handlers.forEach(c=>c(u))
  47. } catch (u) {
  48. logger.error(u)
  49. }
  50. if (n.newUserStates && n.newUserStates.length > 0 && this._room.avatarManager.handleAvatar(n),
  51. o != null && o.playerState) {
  52. this._room._currentClickingState = o.playerState;
  53. const {pathName: u, attitude: c, areaName: h, skinId: f} = o.playerState;
  54. if (u && (this._room.pathManager.currentPathName = u,
  55. this._room.updateCurrentState({
  56. pathName: u
  57. })),
  58. f && this.udpateSkinInfo(f),
  59. h && this._room.updateCurrentState({
  60. areaName: h
  61. }),
  62. c) {
  63. const d = this._room.skin.routeList.find(g=>g.areaName === this._room.currentState.areaName)
  64. , _ = ((d == null ? void 0 : d.step) || 7.5) * 30;
  65. this._room.updateCurrentState({
  66. speed: _,
  67. attitude: c
  68. }),
  69. this._room.pathManager.currentAttitude = c,
  70. this._room._userAvatar && (this._room._userAvatar.motionType = c)
  71. }
  72. this._room.sceneManager.getCurrentShaderMode() !== ECurrentShaderMode.pano && !this._room.isPano && o.playerState.camera && this._room.camera.setCameraPose(o.playerState.camera)
  73. }
  74. if (o != null && o.renderInfo && this._room.camera.handleRenderInfo(o),
  75. n.actionType !== void 0) {
  76. const {actionType: u, code: c, echoMsg: h, traceId: f} = n;
  77. u === Actions.Echo && c === Codes.Success && this._room.networkController.rtcp.heartbeat.pong(h, f),
  78. c !== Codes.Success ? eventsManager.remove(f, c) : [Actions.GetReserveStatus, Actions.Broadcast, Actions.ChangeNickname, Actions.ConfirmEvent, Actions.ReserveSeat, Actions.Rotation, Actions.TurnTo, Actions.RotateTo, Actions.SetPlayerState, Actions.GetNeighborPoints, Actions.TurnToFace, Actions.AudienceChangeToVisitor, Actions.RemoveVisitor, Actions.GetUserWithAvatar].includes(u) && eventsManager.remove(f, c, n)
  79. }
  80. }
  81. handleActionResponses(e) {
  82. !(e != null && e.actionResponses) || e.actionResponses.length === 0 || e.actionResponses.forEach(t=>{
  83. if (t.actionType == null)
  84. return;
  85. const {pointType: r, extra: n, actionType: o, traceId: a, code: s, msg: l} = t;
  86. o === Actions.GetNeighborPoints ? eventsManager.remove(a, s, t.nps) : o === Actions.GetUserWithAvatar ? eventsManager.remove(a, s, t.userWithAvatarList) : eventsManager.remove(a, s, l),
  87. r === PointType.Path && o === Actions.Clicking && (this._room.moveToExtra = decodeURIComponent(n))
  88. }
  89. )
  90. }
  91. async udpateSkinInfo(e) {
  92. this._room.updateCurrentState({
  93. skinId: e
  94. });
  95. const t = await this._room.skinList.find(r=>r.id === e);
  96. t && this._room.updateCurrentState({
  97. skin: t
  98. })
  99. }
  100. }