Signal.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import Codes from "./enum/Codes.js"
  2. import {eventsManager} from "./EventsManager.js"
  3. import ECurrentShaderMode from "./enum/ECurrentShaderMode.js"
  4. import Actions from "./enum/Actions.js"
  5. import PointType from "./enum/PointType.js"
  6. import Logger from "./Logger.js"
  7. import util from "./util.js"
  8. const logger = new Logger('xverse-signal')
  9. export default class Signal {
  10. constructor(e) {
  11. this.signalHandleActived = !0
  12. this.isUpdatedYUV = !0
  13. this._room = e
  14. }
  15. handleSignal(e) {
  16. var a, s, newUserStates;
  17. if (!this.signalHandleActived)
  18. {
  19. return;
  20. }
  21. console.log('执行handleSignal:'+e.signal)
  22. const {signal: signal, alreadyUpdateYUV: alreadyUpdateYUV} = e;
  23. this.handleActionResponses(signal);
  24. //this._room.handleSignalHook(signal);
  25. //视频流没有传送就不能执行
  26. if (!alreadyUpdateYUV) {
  27. const u = (a = signal.newUserStates) == null ? void 0 : a.find(c=>c.userId === this._room.userId);
  28. if ((u == null ? void 0 : u.renderInfo) && ((s = this._room._userAvatar) == null ? void 0 : s.isMoving)) {
  29. logger.debug("stream stoped, make avatar to stop");
  30. const {isMoving: c, isRotating: h} = u.renderInfo;
  31. this._room.avatarManager._updateAvatarMovingStatus({
  32. id: u.userId,
  33. isMoving: !!c,
  34. isRotating: !!h
  35. })
  36. }
  37. return
  38. }
  39. this.isUpdatedYUV = alreadyUpdateYUV;
  40. const receiveSignal = signal;
  41. if (!signal) {
  42. logger.warn("metadata signal is empty");
  43. return
  44. }
  45. if (receiveSignal.code === Codes.RepeatLogin) {
  46. this._room.handleRepetLogin();
  47. return
  48. }
  49. if(receiveSignal.code !== void 0 && receiveSignal.code !== Codes.Success && receiveSignal.code !== Codes.ActionMaybeDelay && receiveSignal.code !== Codes.DoActionBlocked && receiveSignal.code !== Codes.GetOnVehicle){
  50. logger.error("signal errcode: ", receiveSignal)
  51. this._room.emit("error", receiveSignal)
  52. }
  53. if(!window.isUser && receiveSignal.newUserStates){
  54. receiveSignal.newUserStates[0].userId = nickname
  55. receiveSignal.newUserStates[0].playerState.nickName = nickname
  56. window.isUser = true
  57. }
  58. newUserStates = receiveSignal.newUserStates
  59. let currentUserStates = void 0
  60. if(newUserStates != null){
  61. currentUserStates = newUserStates.find(u=>u.userId === this._room.userId);
  62. }
  63. if (receiveSignal.broadcastAction)
  64. {
  65. try {
  66. const u = JSON.parse(receiveSignal.broadcastAction.data);
  67. Broadcast.handlers.forEach(c=>c(u))
  68. } catch (u) {
  69. logger.error(u)
  70. }
  71. }
  72. receiveSignal.newUserStates && receiveSignal.newUserStates.length > 0 && this._room.avatarManager.handleAvatar(receiveSignal);
  73. if (currentUserStates != null && currentUserStates.playerState) {
  74. this._room._currentClickingState = currentUserStates.playerState;
  75. const {pathName: pathName, attitude: attitude, areaName: areaName, skinId: skinId} = currentUserStates.playerState;
  76. if(pathName){
  77. this._room.pathManager.currentPathName = pathName;
  78. this._room.updateCurrentState({
  79. pathName: pathName
  80. })
  81. }
  82. skinId && this.udpateSkinInfo(skinId);
  83. areaName && this._room.updateCurrentState({
  84. areaName: areaName
  85. });
  86. if (attitude) {
  87. const d = this._room.skin.routeList.find(g=>g.areaName === this._room.currentState.areaName)
  88. const speed = ((d == null ? void 0 : d.step) || 7.5) * 30;
  89. this._room.updateCurrentState({
  90. speed: speed,
  91. attitude: attitude
  92. }),
  93. this._room.pathManager.currentAttitude = attitude,
  94. this._room._userAvatar && (this._room._userAvatar.motionType = attitude)
  95. }
  96. if(this._room.sceneManager.getCurrentShaderMode() !== ECurrentShaderMode.pano && !this._room.isPano && currentUserStates.playerState.camera){
  97. // console.error(currentUserStates.playerState)
  98. this._room.camera.setCameraPose(currentUserStates.playerState.camera)
  99. //console.log('接收数据setCameraPose:'+JSON.stringify(currentUserStates.playerState.camera.angle))
  100. //console.log('我们计算:'+ util.ue4Rotation2Xverse(currentUserStates.playerState.camera.angle).y/Math.PI*180)
  101. }
  102. }
  103. currentUserStates != null && currentUserStates.renderInfo && this._room.camera.handleRenderInfo(currentUserStates)
  104. if (receiveSignal.actionType !== void 0) {
  105. const {actionType: actionType, code: code, echoMsg: echoMsg, traceId: traceId} = receiveSignal;
  106. if(actionType === Actions.Echo && code === Codes.Success){
  107. this._room.networkController.rtcp.heartbeat.pong(echoMsg, traceId)
  108. }
  109. if(code !== Codes.Success){
  110. eventsManager.remove(traceId, code)
  111. }
  112. else if([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(actionType)){
  113. eventsManager.remove(traceId, code, receiveSignal)
  114. }
  115. }
  116. }
  117. handleActionResponses(responseData) {
  118. if(responseData != null && responseData.actionResponses&&responseData.actionResponses.length != 0){
  119. responseData.actionResponses.forEach(actionResponse=>{
  120. if (actionResponse.actionType == null)
  121. {
  122. return;
  123. }
  124. const {pointType: pointType, extra: extra, actionType: actionType, traceId: traceId, code: code, msg: msg} = actionResponse;
  125. if(actionType === Actions.GetNeighborPoints){
  126. eventsManager.remove(traceId, code, actionResponse.nps)
  127. }
  128. else if(actionType === Actions.GetUserWithAvatar){
  129. eventsManager.remove(traceId, code, actionResponse.userWithAvatarList)
  130. }
  131. else{
  132. eventsManager.remove(traceId, code, msg)
  133. }
  134. if(pointType === PointType.Path && actionType === Actions.Clicking){
  135. this._room.moveToExtra = decodeURIComponent(extra)
  136. }
  137. })
  138. }
  139. }
  140. async udpateSkinInfo(e) {
  141. this._room.updateCurrentState({
  142. skinId: e
  143. });
  144. const t = await this._room.skinList.find(r=>r.id === e);
  145. t && this._room.updateCurrentState({
  146. skin: t
  147. })
  148. }
  149. }