123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System.Collections;
- using System.Collections.Generic;
- using Blue;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using UnityEngine;
- using UnityEngine.Networking;
- public class SignallingService : ISignallingService
- {
- public void OnInit()
- {
- this.RegisterEvent<GetContactsSuccessEvent>(GetContactsSuccess);
- }
- public void GetContacts()
- {
- CoroutineSystem.Instance.StartCoroutine(OnGetContacts());
- }
- #region 事件
- private void GetContactsSuccess(GetContactsSuccessEvent e)
- {
- string json = JsonConvert.SerializeObject(e.ContactsInfoList);
- Debug.LogError($"获取联系人:{json}");
- }
- #endregion
- #region 协程
- private string message;
- private IEnumerator OnGetContacts()
- {
- UnityWebRequest webRequest = new UnityWebRequest(HttpAction.mEndustryURL + HttpAction.signal_contact, "POST");
-
- webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- webRequest.SetRequestHeader("Authorization", login.UserInfo.Instance.Token);
- webRequest.downloadHandler = new DownloadHandlerBuffer();
- yield return webRequest.SendWebRequest();
- if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- Debug.LogError($"Error:{webRequest.error},DownloadHandler:{webRequest.downloadHandler.text}");
- else
- {
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
- {
- message = webRequest.downloadHandler.text;
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200" && !string.IsNullOrEmpty(jobject["data"].ToString()) && !string.IsNullOrEmpty(jobject["data"]["list"].ToString()))
- {
- message = jobject["data"]["list"].ToString();
- List<ContactsInfo> mContactsInfoList = JsonConvert.DeserializeObject<List<ContactsInfo>>(message);
- this.SendEvent(new GetContactsSuccessEvent() { ContactsInfoList = mContactsInfoList });
- }
- }
- }
- }
- #endregion
- }
|