public static IEnumerator Post(string url, CSLinkInfo csLinkInfo, int timeout, Action <string, int, long> OnSucc, Action <WebErrorCode> OnError) { byte[] byteData = ProtoSerAndUnSer.Serialize(csLinkInfo); CSServerReq req = new CSServerReq { Data = ByteString.AttachBytes(byteData), OpCode = OpCodeType.GetConnectServer }; byte[] sendData = ProtoSerAndUnSer.Serialize(req); WWWForm from = new WWWForm(); UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST) { uploadHandler = new UploadHandlerRaw(sendData), downloadHandler = new DownloadHandlerBuffer(), timeout = timeout }; request.SetRequestHeader("Content-Type", "multipart/form-data"); yield return(request.SendWebRequest()); //拿到信息 SCServerRes data = ProtoSerAndUnSer.UnSerialize <SCServerRes>(request.downloadHandler.data); if (data == null) { Debug.LogError("网关异常"); OnError?.Invoke(new WebErrorCode()); yield break; } if (data.Data != null && data.Code == (int)WebErrorCode.Success) { SCLinkInfo info = ProtoSerAndUnSer.UnSerialize <SCLinkInfo>(data.Data.ToByteArray()); Debug.Log(info); if (info != null) { string ip = StaticData.IntToIp((long)info.Ip); OnSucc?.Invoke(ip, info.Port, info.Uid); } } else { OnError?.Invoke((WebErrorCode)data.Code); } }
/// <summary> /// 请求短链接 /// </summary> /// <param name="sole"> 授权使用的唯一id </param> /// <param name="platform">授权平台</param> private async void RequestShortLink(string sole = null, RegisterPlatform platform = RegisterPlatform.None) { if (_isStartRequst) { return; } Debug.Log("请求短链接 RequestShortLink _typeGateWay =" + _typeGateWay); // 设置请求短链接参数 //加载网关 TextAsset gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfo"); switch (_typeGateWay) { case TypeGateWay.Stable: gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfo"); break; case TypeGateWay.MZQ: gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfoMZQ"); break; case TypeGateWay.YQX: gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfoYQX"); break; case TypeGateWay.JLF: gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfoJLF"); break; case TypeGateWay.Extranet: gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfoExtranet"); if (StaticData.isUsePlatformUpdatingGateWay) { gatewayInfo = await ABManager.GetAssetAsync <TextAsset>("GateWayInfoExtranetUpdating"); } break; } GateWayInfo gateWayInfo = LitJson.JsonMapper.ToObject <GateWayInfo>(gatewayInfo.text); StaticData.DebugGreen($"gateWayInfo ip:{gateWayInfo.ipGateWay} port:{gateWayInfo.portGateWay} timeout:{gateWayInfo.timeout}"); // 0 等待中 1 请求成功 2 请求失败 int gateWayReturn = 0; string urlGateWay = $"{gateWayInfo.ipGateWay}:{gateWayInfo.portGateWay}"; if (!StaticData.IsUsedLocalDataNotServer) { CSLinkInfo csLinkInfo = new CSLinkInfo();// { Sole = SystemInfo.deviceUniqueIdentifier}; if (platform != RegisterPlatform.None) { csLinkInfo.Sole = sole; //授权使用的唯一id csLinkInfo.Platform = platform; //授权平台 } else { csLinkInfo.Sole = SystemInfo.deviceUniqueIdentifier + GetComputerName(); Debug.LogWarning($"==============csLinkInfo.Sole:{csLinkInfo.Sole}"); } Console.WriteLine("OnHeartbeatUpdate: Ping2"); _isStartRequst = true; SetLoginButInteractable(false); //发起链接请求 StartCoroutine(HttpGateWayManager.Post(urlGateWay, csLinkInfo, gateWayInfo.timeout, (ip, port, Uid) => { StaticData.ipWebSocket = ip; StaticData.portWebSocket = port; StaticData.Uid = Uid; StaticData.DebugGreen($"玩家的uid:{Uid}"); gateWayReturn = 1; }, (WebErrorCode webErrorCode) => { StaticData.DebugGreen($"webErrorCode:{webErrorCode.ToString()}"); gateWayReturn = 2; })); await UniTask.WaitUntil(() => gateWayReturn != 0); if (gateWayReturn == 1) { //进行长连接 使用WebSocket RequestLongLink(); } else { //打开提示服务器正在维护中 OpenTipesServerIsUnderMaintenance(); _isStartRequst = false; SetLoginButInteractable(true); } } }