123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using static LangChaoRommMinIo;
- public class MQTTManager : MonoSingleton<MQTTManager>
- {
- public string front = "client/";
- public string id = "";
- public string username = "u@unity3";// 需要根据服务器设置
- public string password = null;// 需要根据服务器设置
- public string rid = "mqttx_b4c02ddc"; //其他人的ID
- public string account;
- public string roomId;
- public string email;
- public string phone;
- public string roleId;
- public string resourcePool;
- public string expertType;
- public string _username;
- public string room;
- public static string certification;
- /// <summary>
- /// 获取信令
- /// </summary>
- public void Signal()
- {
- GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.signal_certification, "", SignalCallBack));
- // GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
- // HttpTool.Instance.PostMqtt(HttpAction.signal_certification, "", SignalCallBack);
- }
- public void SetAccoutInfo(string account)
- {
- MQTTManager.Instance.account = account;
- }
- private void SignalCallBack(string msg)
- {
- Debug.Log(msg);
- JObject obj = JObject.Parse(msg);
- if (obj["code"].ToString() == "200")
- {
- HttpActionLang.Instance.mqttSocket = obj["data"]["host"].ToString();
- MQTTManager.certification = obj["data"]["certification"].ToString();
- MQTTManager.Instance.front = obj["data"]["topicPrefix"].ToString();
- MQTTManager.Instance.id = obj["data"]["clientId"].ToString();
- MQTTManager.Instance.username = obj["data"]["username"].ToString();
- MQTTClient.Instance.Connect();
- }
- else
- {
- Debug.LogError("MQTT 获取信令失败 !!!" + obj["code"].ToString());
- }
- }
- private void UserInfoCallBack(string msg)
- {
- Debug.Log(msg);
- JObject obj = JObject.Parse(msg);
- if (obj["code"].ToString() == "200")
- {
- MQTTManager.Instance._username = obj["data"]["name"].ToString();
- MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
- MQTTManager.Instance.email = obj["data"]["email"].ToString();
- MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
- MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
- MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
- }
- else
- {
- Debug.LogError("获取用户信息失败 !!!"+ obj["code"].ToString());
- }
- }
- public void SetRoomId(string id)
- {
- MQTTManager.Instance.roomId = id;
- // MQTTClient.Instance.roomId = "9527";
- // 订阅房间频道
- MQTTClient.Instance.Subscribe(MQTTManager.Instance.front + "room/" + id);
-
- }
-
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="msg"></param>
- public void PushMsg(string msg)
- {
- if (!string.IsNullOrEmpty(msg))
- {
- JsonData json = new JsonData();
- json["account"] = MQTTManager.Instance.account;
- json["Sid"] = "sid";//选择的场景id
- json["msg"] = msg;
- MQTTClient.Instance.publish(Encoding.UTF8.GetBytes(json.ToJson()));
-
- }
- }
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="msg"></param>
- public void PushMsg(string channelID, string msg)
- {
- if (!string.IsNullOrEmpty(msg))
- {
- MQTTClient.Instance.publish(channelID, Encoding.UTF8.GetBytes(msg));
- }
- }
- /// <summary>
- /// 接受消息
- /// </summary>
- /// <param name="msg"></param>
- public void Received(string msg)
- {
- JObject obj = JObject.Parse(msg);
- Debug.Log("Received ===> " + obj["action"].ToString());
- switch (obj["action"].ToString())
- {
- case "timeout": // 超时
- UICallManager.Instance.EventTimeOut();
- break;
- case "invite": // 邀请
- if(obj["control"] !=null)
- {
- //string roomid = obj["rtcRoom"].ToString();
- string phone = obj["control"]["phone"].ToString();
- //string resourcePool = obj["control"]["resourcePoo1"].ToString();
- UICallManager.Instance.EventInvite(obj["control"].ToString(),"");
- Debug.Log(phone);
- RTCRoomManager.Instance.controlPhone = phone;
-
- }
- else
- {
- Debug.LogError(" 非控制台邀请 ");
- }
-
- break;
- case "noExpert": // 无专家
- UICallManager.Instance.EventNoExpert();
- break;
- case "chat": // 聊天消息
- // 拆解用户名 和 聊天内容 显示到聊天框里
- string username = obj["from"].ToString();
- string chatMsg = obj["chatMessage"].ToString();
- FileList.Instance.AddMsgItem(null, username, chatMsg);
- break;
- case "share": // 接受文件
- // 拆解用户名 和 文件名称 显示到聊天框里 下载文件
- FileConfig fileConfig = JsonConvert.DeserializeObject<FileConfig>(obj["chatMessage"].ToString());
- fileConfig.From = obj["from"].ToString();
- Debug.Log(" DGJ ===> FileConfig " + JsonConvert.SerializeObject(fileConfig));
-
- FileList.Instance.AddItem(fileConfig);
- break;
- case "remark": // 空间标注
- float Valuex = float.Parse(obj["chatMessage"]["x"].ToString());
- float Valuey = float.Parse(obj["chatMessage"]["y"].ToString());
- ArrowList.Instance.AddArrow(Valuex, Valuey);
- break;
- case "clearRemark":
- ArrowList.Instance.DeleteAll();
- break;
- default:
- break;
- }
- }
- }
|