/// <summary> /// 离开房间 /// </summary> /// <param name="rid">房间id</param> public void LeaveRoom(string rid) { MqttHelper mqttHelper = ChooseMqtt(rid); if (mqttHelper == null) { return; } //mqttHelper.UnSubscribeMsg(rid); //mqttHelper.UnSubscribeMsg(uid); LeaveRoom leaveRoom = new LeaveRoom(); leaveRoom.id = (int)NetCmdId.LeaveRoom; leaveRoom.data = new LeaveRoom.ResultBean(); leaveRoom.data.rid = rid; leaveRoom.data.uid = uid; string json = JsonConvert.SerializeObject(leaveRoom); mqttHelper.PublishMsg(MessageType.System, SystemTargetId, json, new MqttSendMsgDelegate((bool res) => { mqttHelper.Disconnect(); mqttHelper = null; })); }
/// <summary> /// 同步动画 /// </summary> /// <param name="type">类型</param> /// <param name="objId">对象id, 数字人时穿null</param> /// <param name="animId">动画id</param> public void SyncAnimation(SyncType type, string objId, int animId) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SyncAnim syncAnim = new SyncAnim(); syncAnim.type = type; if (type == SyncType.VirtualMan) { syncAnim.id = this.uid; } else { syncAnim.id = objId; } syncAnim.amimid = animId; syncAnim.time = TimeHelper.GetTimestamp(); byte[] cmd = EncodeStruct <SyncAnim>(syncAnim); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncAnim, cmd); }
/// <summary> /// 同步位置 /// </summary> /// <param name="targetId">目标id</param> /// <param name="type">同步类型</param> /// <param name="objId">对象id</param> /// <param name="position">位置</param> public void SyncPosition(string targetId, SyncType type, string objId, Vector3 position) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SyncPos headPos = new SyncPos(); headPos.type = type; headPos.id = objId; headPos.px = position.x; headPos.py = position.y; headPos.pz = position.z; headPos.time = TimeHelper.GetTimestamp(); //Debug.Log("SyncPosition type:" + type + "objid:" + objId + " pos:" + position + " time:" + headPos.time); if (_lastPosTime >= headPos.time) { //Debug.Log("SyncPosition 顺序错误 cur:" + headPos.time + " last:" + _lastPosTime); } _lastPosTime = headPos.time; byte[] cmd = EncodeStruct <SyncPos>(headPos); if (targetId == null) { SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncPos, cmd); } else { SendCmdMessage(mqttHelper, targetId, NetCmdIdClient.SyncPos, cmd); } }
/// <summary> /// Syncs the rotation. /// </summary> /// <returns>The rotation.</returns> /// <param name="targetId">Target identifier.</param> /// <param name="type">Type.</param> /// <param name="objId">Object identifier.</param> /// <param name="rotation">Rotation.</param> public void SyncRotation(string targetId, SyncType type, string objId, Vector3 rotation) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SyncRotate headRotate = new SyncRotate(); headRotate.type = type; headRotate.id = objId; headRotate.rx = rotation.x; headRotate.ry = rotation.y; headRotate.rz = rotation.z; headRotate.time = TimeHelper.GetTimestamp(); //Debug.Log("SyncRotation type:" + type + "objid:" + objId + " rot:" + rotation + " time:" + headRotate.time); if (_lastRotTime >= headRotate.time) { Debug.Log("SyncRotation 顺序错误 cur:" + headRotate.time + " last:" + _lastRotTime); } _lastRotTime = headRotate.time; byte[] cmd = EncodeStruct <SyncRotate>(headRotate); if (targetId == null) { SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncRotate, cmd); } else { SendCmdMessage(mqttHelper, targetId, NetCmdIdClient.SyncRotate, cmd); } }
/// <summary> /// 根据链接状态, 优先选择数字人服务器 /// </summary> /// <param name="rid">返回的房间id</param> /// <returns>返回的mqtt</returns> private MqttHelper ChooseMqtt(SyncType type, out string rid) { MqttHelper mqttHelper = null; rid = ""; if (type == SyncType.VirtualMan && virtualManNetState == NetState.Connect) { mqttHelper = virtualManMqtt; rid = virtualManRid; } else if (type == SyncType.SpectatorView && spectatorViewNetState == NetState.Connect) { mqttHelper = spectatorViewMqtt; rid = spectatorViewRid; } else if (type == SyncType.Other) { if (virtualManNetState == NetState.Connect) { mqttHelper = virtualManMqtt; rid = virtualManRid; } else if (spectatorViewNetState == NetState.Connect) { mqttHelper = spectatorViewMqtt; rid = spectatorViewRid; } } return(mqttHelper); }
private void Mqtt_onConnectStateChange(MqttHelper t, ConnectState m) { MYDialog.Instance.Messagequeue.Enqueue(m); if (m == ConnectState.Connected) { } else { string roomId = ""; if (t == spectatorViewMqtt) { roomId = spectatorViewRid; IsInSpectatorViewRoom = false; spectatorViewNetState = NetState.Disconnect; } else { IsInVirtualManRoom = false; roomId = virtualManRid; virtualManNetState = NetState.Disconnect; } onDisconnect?.Invoke(roomId); t.OnReceiveMsg -= Mqtt_OnReceiveMsg; t.onConnectStateChange -= Mqtt_onConnectStateChange; t.OnReceiveByteMsg -= Mqtt_OnReceiveCmdMsg; } }
private void Mqtt_OnReceiveCmdMsg(MqttHelper mqtt, byte[] msg) { string roomId = ""; if (mqtt == spectatorViewMqtt) { roomId = spectatorViewRid; } else { roomId = virtualManRid; } onReceiveCmdFunc(roomId, msg); }
private MqttHelper ChooseMqtt(string rid) { MqttHelper mqttHelper = null; if (rid.Equals(spectatorViewRid)) { mqttHelper = spectatorViewMqtt; } else if (rid.Equals(virtualManRid)) { mqttHelper = virtualManMqtt; } return(mqttHelper); }
public void MarkerGenerated() { MqttHelper mqttHelper = spectatorViewMqtt; string rid = spectatorViewRid; if (spectatorViewNetState != NetState.Connect) { return; } MarkerGenerated cmd = new MarkerGenerated(); cmd.senderid = uid; byte[] cmdBuf = EncodeStruct <MarkerGenerated>(cmd); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.MarkerGenerated, cmdBuf); }
/// <summary> /// 同步其他类型命令 /// </summary> /// <param name="id"></param> /// <param name="param"></param> public void SyncOtherCmd(string id, string param) { string rid; MqttHelper mqttHelper = ChooseMqtt(SyncType.Other, out rid); if (mqttHelper == null) { return; } OtherCmd otherCmd = new OtherCmd(); otherCmd.id = id; otherCmd.cmd = param; byte[] cmd = EncodeStruct <OtherCmd>(otherCmd); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.OtherCmd, cmd); }
/// <summary> /// 菜单选择 /// </summary> /// <param name="menuId">菜单id</param> /// <param name="itemIndex">选择的菜单项id</param> public void MenuSelectItem(string menuId, int itemIndex) { string rid; MqttHelper mqttHelper = ChooseMqtt(SyncType.Other, out rid); if (mqttHelper == null) { return; } MenuSelectItem menuSelectItem = new MenuSelectItem(); menuSelectItem.mid = menuId; menuSelectItem.itemIdx = itemIndex; byte[] cmd = EncodeStruct <MenuSelectItem>(menuSelectItem); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.MenuSelectItem, cmd); }
private void SendCmdMessage(MqttHelper mqttHelper, string rid, NetCmdIdClient cid, byte[] cmd) { //SendCommend sendCommend = new SendCommend(); //sendCommend.id = (int)NetCmdId.SendCmmond; //sendCommend.data = new SendCommend.ResultBean(); //sendCommend.data.cmd = cmd; //string json = JsonConvert.SerializeObject(sendCommend); byte[] bId = BitConverter.GetBytes((int)cid); byte[] sendBuf = new byte[4 + cmd.Length]; Array.Copy(bId, 0, sendBuf, 0, bId.Length); Array.Copy(cmd, 0, sendBuf, 4, cmd.Length); bId = null; cmd = null; mqttHelper.PublishMsg(MessageType.Group, rid, sendBuf, new MqttSendMsgDelegate((bool res) => { })); }
/// <summary> /// 菜单显示隐藏,也可用于其他物体 /// </summary> /// <param name="menuId">菜单id</param> /// <param name="showHide">是否显示</param> public void MenuShowHide(string menuId, bool showHide) { string rid; MqttHelper mqttHelper = ChooseMqtt(SyncType.Other, out rid); if (mqttHelper == null) { return; } MenuShowHide menuShowHide = new MenuShowHide(); menuShowHide.mid = menuId; menuShowHide.sh = showHide; byte[] cmd = EncodeStruct <MenuShowHide>(menuShowHide); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.MenuShowHide, cmd); }
public void SyncWorldRoot(int markerid, float posx, float posy, float posz, float angley) { MqttHelper mqttHelper = spectatorViewMqtt; string rid = spectatorViewRid; if (spectatorViewNetState != NetState.Connect) { return; } SyncWorldRoot cmd = new SyncWorldRoot(); cmd.senderid = uid; cmd.markerid = markerid; cmd.posx = posx; cmd.posy = posy; cmd.posz = posz; cmd.angley = angley; byte[] cmdBuf = EncodeStruct <SyncWorldRoot>(cmd); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncWorldRoot, cmdBuf); }
public void GetRoomCacheFromServer(SyncType type) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } GetRoomCache getRoomCache = new GetRoomCache(); getRoomCache.id = (int)NetCmdId.GetRoomCache; getRoomCache.data = new GetRoomCache.ResultBean(); getRoomCache.data.rid = rid; getRoomCache.data.uid = this.uid; string json = JsonConvert.SerializeObject(getRoomCache); mqttHelper.PublishMsg(MessageType.System, SystemTargetId, json, new MqttSendMsgDelegate((bool res) => { })); }
public void SaveSVRoomCacheToServer(string key, string value) { string rid = spectatorViewRid; MqttHelper mqttHelper = spectatorViewMqtt; if (spectatorViewNetState != NetState.Connect) { return; } SaveRoomCache saveRoomCache = new SaveRoomCache(); saveRoomCache.id = (int)NetCmdId.SaveRoomCache; saveRoomCache.data = new SaveRoomCache.ResultBean(); saveRoomCache.data.rid = rid; saveRoomCache.data.key = key; saveRoomCache.data.value = value; string json = JsonConvert.SerializeObject(saveRoomCache); mqttHelper.PublishMsg(MessageType.System, SystemTargetId, json, new MqttSendMsgDelegate((bool res) => { })); }
public void SaveRoomCacheToServer(SyncType type, string key, string value) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SaveRoomCache saveRoomCache = new SaveRoomCache(); saveRoomCache.id = (int)NetCmdId.SaveRoomCache; saveRoomCache.data = new SaveRoomCache.ResultBean(); saveRoomCache.data.rid = rid; saveRoomCache.data.key = key; saveRoomCache.data.value = value; string json = JsonConvert.SerializeObject(saveRoomCache); mqttHelper.PublishMsg(MessageType.System, SystemTargetId, json, new MqttSendMsgDelegate((bool res) => { })); }
/// <summary> /// 加入房间 /// </summary> /// <param name="uid">用户id</param> /// <param name="rid">房间id</param> /// <param name="clientType">客户端类型</param> /// <param name="roomType">房间类型</param> public void JoinRoom(string uid, string rid, ClientType clientType, RoomType roomType) { this.uid = uid; MqttHelper mqtt = null; string sip = ""; int sport = 0; string clientid = ""; if (roomType == RoomType.SpeactorView) { this.spectatorViewRid = rid; sip = SyncInterface.Instance.MrServerAddress; sport = ServerUrls.MqttPort; clientid = "_sv"; if (spectatorViewNetState == NetState.Connect) { return; } if (spectatorViewMqtt != null) { spectatorViewMqtt = null; } spectatorViewMqtt = new MqttHelper(); mqtt = spectatorViewMqtt; } else if (roomType == RoomType.VirtualMan) { this.virtualManRid = rid; sip = SyncInterface.Instance.MrServerAddress; sport = ServerUrls.MqttPort; clientid = "_vm"; if (virtualManNetState == NetState.Connect) { return; } if (virtualManMqtt != null) { virtualManMqtt = null; } virtualManMqtt = new MqttHelper(); mqtt = virtualManMqtt; } if (mqtt == null) { onJoinRoom?.Invoke(rid, false, false, null, null); return; } mqtt.onConnectStateChange += Mqtt_onConnectStateChange; mqtt.OnReceiveMsg += Mqtt_OnReceiveMsg; mqtt.OnReceiveByteMsg += Mqtt_OnReceiveCmdMsg; mqtt.Connect(sip, sport, uid, "111", uid + clientid, new MqttConnectDelegate( (MqttHelper t, bool result) => { if (result) { mqtt.SubscribeMsg(rid, new ResultDelegate <bool>((bool subResult) => { })); mqtt.SubscribeMsg(uid, new ResultDelegate <bool>((bool subResult) => { })); if (roomType == RoomType.SpeactorView) { spectatorViewNetState = NetState.Connect; } else { virtualManNetState = NetState.Connect; } // TODO: send join room JoinRoom join = new JoinRoom(); join.id = (int)NetCmdId.JoinRoom; join.data = new JoinRoom.ResultBean(); join.data.uid = uid; join.data.rid = rid; join.data.role = (int)clientType; string json = JsonConvert.SerializeObject(join); t.PublishMsg(MessageType.System, SystemTargetId, json, new MqttSendMsgDelegate((bool res) => { //int i = 0; })); } else { onJoinRoom?.Invoke(rid, false, false, null, null); } })); }
private void Mqtt_OnReceiveMsg(MqttHelper mqtt, string msg) { Debug.Log("Mqtt_OnReceiveMsg:" + msg); string roomId = ""; if (mqtt == spectatorViewMqtt) { roomId = spectatorViewRid; } else { roomId = virtualManRid; } CmdHeader header = JsonConvert.DeserializeObject <CmdHeader>(msg); switch ((NetCmdId)header.id) { case NetCmdId.None: break; case NetCmdId.JoinRoom: //加入房间请求 break; case NetCmdId.JoinRoomRes: //加入房间应答 if (mqtt == spectatorViewMqtt) { IsInSpectatorViewRoom = true; } else { IsInVirtualManRoom = true; } JoinRoomRes joinRoomRes = JsonConvert.DeserializeObject <JoinRoomRes>(msg); onJoinRoom?.Invoke(roomId, true, joinRoomRes.data.isCreator == (int)YesOrNot.YES, joinRoomRes.data.roomCache, joinRoomRes.data.uids); break; case NetCmdId.LeaveRoom: //离开房间请求 break; case NetCmdId.UserEnterRoom: //用户加入房间通知 UserEnterRoom userEnterRoom = JsonConvert.DeserializeObject <UserEnterRoom>(msg); onUserEnter?.Invoke(roomId, userEnterRoom.data.uid); break; case NetCmdId.UserLeaveRoom: //用户离开房间通知 UserLeaveRoom userLeaveRoom = JsonConvert.DeserializeObject <UserLeaveRoom>(msg); onUserLeave?.Invoke(roomId, userLeaveRoom.data.uid); break; case NetCmdId.SendCmmond: //发送命令 //SendCommend sendCommend = JsonConvert.DeserializeObject<SendCommend>(msg); //onReceiveCmdFunc(roomId, sendCommend.data.cmd); break; case NetCmdId.SaveRoomCache: //锚点数据上传完成 break; case NetCmdId.GetRoomCacheRes: GetRoomCacheRes cmd = JsonConvert.DeserializeObject <GetRoomCacheRes> (msg); onRoomCache?.Invoke(cmd.data.rid, cmd.data.roomCache); break; } }