コード例 #1
0
    protected override void InitComponents()
    {
        UICommonWait.Show();
        Task.Run(async() =>
        {
            if (!LibClient.AClientApp.bConnected)
            {
                await LibClient.AClientApp.StartClient();
            }
            UEventListener.Instance.AddProducingAction(() =>
            {
                UICommonWait.Hide();
            });
        });

        inputUsername = FindWidget <InputField>("inputUsername");
        if (!string.IsNullOrEmpty(CachedUsername))
        {
            inputUsername.text = CachedUsername;
        }

        inputPassword = FindWidget <InputField>("inputPassword");
        btnLogin      = FindWidget <Button>("btnLogin");
        btnLogin.onClick.AddListener(() =>
        {
            Task.Run(async() =>
            {
                if (!LibClient.AClientApp.bConnected)
                {
                    await LibClient.AClientApp.StartClient();
                }

                if (!LibClient.AClientApp.bConnected)
                {
                    UEventListener.Instance.AddProducingAction(() =>
                    {
                        btnLogin.enabled = true;

                        UICommonWait.Hide();
                    });
                    AOutput.Log($"连接失败!");
                    return;
                }

                UEventListener.Instance.AddProducingAction(OnLogin);
            });
            btnLogin.enabled = false;
        });
        var btnRegister = FindWidget <Button>("btnRegister");

        btnRegister.onClick.AddListener((UnityEngine.Events.UnityAction)(() =>
        {
            base.OnUnloadThis();

            AHotBase.LoadUI <UIRegister>();
        }));

        RegisterEvent(UEvents.Login, OnLoginCb);
    }
コード例 #2
0
    public static void OnDownloadBytes(string url, string scontent, Action <byte[]> actionResult, Action <string> actionFailed = null)
    {
        UICommonWait.Show();
        new Thread(new ThreadStart(() =>
        {
            try
            {
                if (!string.IsNullOrEmpty(scontent))
                {
                    url = $"{url}?{scontent}";
                }
                var req    = (HttpWebRequest)HttpWebRequest.Create(url);
                req.Method = "GET";
                using (var wr = req.GetResponse() as HttpWebResponse)
                {
                    var resStream = wr.GetResponseStream();                              //获得Response的流

                    int count  = (int)resStream.Length;
                    int offset = 0;
                    var buf    = new byte[count];
                    while (count > 0)
                    {
                        int n = resStream.Read(buf, offset, count);
                        if (n == 0)
                        {
                            break;
                        }
                        count  -= n;
                        offset += n;
                    }

                    callbacksProducing.Add(() =>
                    {
                        if (actionResult != null)
                        {
                            actionResult(buf);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Debug.Log($"web sender error:{ex.Message}\r\n{ex.StackTrace}");
                //lock (lockObj)
                {
                    callbacksProducing.Add(() =>
                    {
                        if (actionFailed != null)
                        {
                            actionFailed(ex.Message);
                        }
                    });
                }
            }
        }
                                   )).Start();
    }
コード例 #3
0
    private void OnCreateOrderCb(UEventBase obj)
    {
        var eb = obj as EventCreateOrder;

        AOutput.Log($"obj {eb.eResult} {eb.orderID} {eb.extraInfo}");
        //AClientApis.OnPay(1);

        UICommonWait.Show();
        OnSendSDKMessage("SDK_AppleInApp", "OnBuyProduct", "0");
    }
コード例 #4
0
    protected override void InitComponents()
    {
        UICommonWait.Show();
        Task.Run(async() =>
        {
            if (!LibClient.AClientApp.bConnected)
            {
                await LibClient.AClientApp.StartClient();
            }
            UEventListener.Instance.AddProducingAction(() =>
            {
                UICommonWait.Hide();
            });
        });

        inputUsername = FindWidget <InputField>("inputUsername");
        if (!string.IsNullOrEmpty(CachedUsername))
        {
            inputUsername.text = CachedUsername;
        }

        inputPassword = FindWidget <InputField>("inputPassword");
        btnLogin      = FindWidget <Button>("btnLogin");
        btnLogin.onClick.AddListener(() =>
        {
            Task.Run(async() =>
            {
                if (!LibClient.AClientApp.bConnected)
                {
                    await LibClient.AClientApp.StartClient();
                }

                if (!LibClient.AClientApp.bConnected)
                {
                    AOutput.Log($"连接失败!");
                    return;
                }

                UEventListener.Instance.AddProducingAction(OnLogin);
            });
            btnLogin.enabled = false;
        });
        var btnRegister = FindWidget <Button>("btnRegister");

        btnRegister.onClick.AddListener(() =>
        {
            OnUnloadThis();

            LoadAnotherUI <UIRegister>();
        });

        UEventListener.Instance.OnRegisterEvent(UEvents.LoginFailed, OnLoginFailedCb);
        UEventListener.Instance.OnRegisterEvent(UEvents.EnterGame, OnEnterGameCb);
        UEventListener.Instance.OnRegisterEvent(UEvents.ServerDisconnected, OnServerDisconnectedCb);
    }
コード例 #5
0
    public static void LoadUI <T>(Action <T> action = null) where T : AHotBase, new()
    {
        UICommonWait.Show();
        var path = "UI/" + typeof(T).Name;

        UHotAssetBundleLoader.Instance.OnDownloadResources(() =>
        {
            UICommonWait.Hide();
            LoadClass <T>(path, action);
        }, path);
    }
コード例 #6
0
 private void AddGame <T>(string gameLabel) where T : AHotBase, new()
 {
     _dGames.Add(gameLabel, () =>
     {
         var gamename = typeof(T).Name;
         UICommonWait.Show();
         UHotAssetBundleLoader.Instance.OnDownloadResources(() =>
         {
             UICommonWait.Hide();
             LoadGame <T>("Game/" + gamename);
         }, "game/" + gamename);
     });
 }
コード例 #7
0
 public void OnRequest(string url, string scontent, Action <string> actionResult, Action <string> actionFailed)
 {
     UICommonWait.Show();
     Debug.Log("request " + url + "?" + scontent);
     new Thread(new ThreadStart(() =>
     {
         try
         {
             byte[] bs         = Encoding.ASCII.GetBytes(scontent);
             var req           = (HttpWebRequest)HttpWebRequest.Create(url);
             req.Method        = "POST";
             req.ContentType   = "application/x-www-form-urlencoded";
             req.ContentLength = bs.Length;
             using (Stream reqStream = req.GetRequestStream())
             {
                 reqStream.Write(bs, 0, bs.Length);
             }
             using (WebResponse wr = req.GetResponse())
             {
                 var result = new StreamReader(wr.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                 Debug.Log("web result " + result);
                 lock (lockObj)
                 {
                     callbacks.Add(() =>
                     {
                         if (actionResult != null)
                         {
                             actionResult(result);
                         }
                     });
                 }
             }
         }
         catch (Exception ex)
         {
             Debug.Log("web sender error:" + ex.Message);
             lock (lockObj)
             {
                 callbacks.Add(() =>
                 {
                     if (actionFailed != null)
                     {
                         actionFailed(ex.Message);
                     }
                 });
             }
         }
     }
                                )).Start();
 }
コード例 #8
0
    private void OnCreateOrderCb(UEventBase obj)
    {
        var eb = obj as EventCreateOrder;

        AOutput.Log($"obj {eb.eResult} {eb.orderID} {eb.extraInfo}");
        //AClientApis.OnPay(1);

        UICommonWait.Show();
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            OnSendSDKMessage("SDK_AppleInApp", "OnBuyProduct", "0");
        }
        else
        {
            OnSendSDKMessage("SDK_WeChat", "OpenWechatPay", eb.extraInfo);
        }
    }
コード例 #9
0
 protected override void InitComponents()
 {
     UICommonWait.Show();
     WebSocketConnector.Instance.OnInit(Utils.WebSocketURL + "username="******"&token=" + UILogin.token
                                        , (openEvt) =>
     {
         UICommonWait.Hide();
         LoadAnotherUI <UMUIMain>();
     }, (msgEvt) =>
     {
     }, (errorEvt) =>
     {
         UIAlert.Show("WSError:" + errorEvt.Message);
     }, (closeMsg) =>
     {
         UICommonWait.Hide();
     });
 }
コード例 #10
0
    private void OnCreateOrderCb(UEventBase obj)
    {
        var eb = obj as EventCreateOrder;

        AOutput.Log($"obj {eb.eResult} {eb.orderID} {eb.extraInfo}");
        //AClientApis.OnPay(1);

        UICommonWait.Show();
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            OnSendSDKMessage("SDK_AppleInApp", "OnBuyProduct", "0");
        }
        else
        {
            var jo = Newtonsoft.Json.JsonConvert.DeserializeObject(eb.extraInfo) as JObject;
            OnSendSDKMessage("SDK_WeChat", "OpenWechatPay", $"{jo["prepayid"]},{jo["noncestr"]},{jo["timestamp"]},{jo["sign"]}");
        }
    }
コード例 #11
0
 public void OnClose()
 {
     if (!bConnected)
     {
         return;
     }
     bConnected       = false;
     this.closeAction = null;
     UnityEngine.Debug.Log("ws close on purpose.");
     if (ws != null)
     {
         UICommonWait.Show();
         new Task(() =>
         {
             ws.Close();
             callbackAdd.Add(() =>
             {
                 UICommonWait.Hide();
             });
         }).Start();
     }
 }
コード例 #12
0
    public void OnInit(string wsurl, Action <EventArgs> openAction, Action <MessageEventArgs> msgAction
                       , Action <ErrorEventArgs> errorAction, Action <string> closeAction)
    {
        if (bConnecting ||
            bConnected)
        {
            return;
        }
        this.closeAction = closeAction;

        ws         = new WebSocket(wsurl);
        ws.OnOpen += (sender, e) =>
        {
            UnityEngine.Debug.Log("OnOpen:" + wsurl);
            bConnecting = false;
            bConnected  = true;
            lock (lockObj)
            {
                callbackAdd.Add(() =>
                {
                    UICommonWait.Hide();
                    openAction(e);
                });
            }
            receive = true;
        };
        ws.OnMessage += (sender, e) =>
        {
            if (e.Data == "ping")
            {
                receive = true;
            }
            else
            {
                UnityEngine.Debug.Log("OnMessage:" + (e.IsText ? e.Data : e.RawData.Length.ToString()));
                lock (lockObj)
                {
                    callbackAdd.Add(() =>
                    {
                        msgAction(e);

                        var amsg = e.Data.Split('?');
                        if (amsg.Length > 1)
                        {
                            OnResponse(amsg[0], amsg[1]);
                        }
                    });
                }
            }
        };
        ws.OnError += (sender, e) =>
        {
            UnityEngine.Debug.Log("OnError:" + e.Message);
            lock (lockObj)
            {
                callbackAdd.Add(() =>
                {
                    errorAction(e);
                });
            }
        };
        ws.OnClose += (sender, e) =>
        {
            bConnecting = false;
            bConnected  = false;

            closeAction?.Invoke(e.Reason);
            UnityEngine.Debug.Log("OnClose:" + e.Reason);
        };
        bConnecting = true;
        UnityEngine.Debug.Log("ws.Connect() " + wsurl);
        UICommonWait.Show();
        new Thread(new ThreadStart(() =>
        {
            try
            {
                ws.Connect();
            }
            catch
            {
                UnityEngine.Debug.Log("connect failed:" + wsurl);
            }
        })).Start();
    }
コード例 #13
0
    protected override void InitComponents()
    {
        mycard = FindWidget <RawImage>("mycard");
        mycard.gameObject.SetActive(false);
        var btn = mycard.gameObject.AddComponent <Button>();

        btn.onClick.AddListener(() =>
        {
            if (whosTurn != UILogin.CachedUsername)
            {
                UICommonTips.AddTip("还没轮到你出牌。");
                return;
            }
            mycard.color = Color.grey;
            WebSocketConnector.Instance.OnRemoteCall("roomOp", "");
        });
        othercard = FindWidget <RawImage>("othercard");
        othercard.gameObject.SetActive(false);
        var bgpath = new string[] { "Images/Pai/bg1", "Images/Pai/bg2" };

        UHotAssetBundleLoader.Instance.OnDownloadResources(() =>
        {
            mycard.texture    = UHotAssetBundleLoader.Instance.OnLoadAsset <Texture2D>(bgpath[random.Next(bgpath.Length)]);
            othercard.texture = UHotAssetBundleLoader.Instance.OnLoadAsset <Texture2D>(bgpath[random.Next(bgpath.Length)]);
        }, bgpath);

        textMyCardCount      = FindWidget <Text>("textMyCardCount");
        textMyCardCount.text = "0";

        textMyAvatarname      = FindWidget <Text>("textMyAvatarname");
        textMyAvatarname.text = URemoteData.AvatarName;

        textMyLevel = FindWidget <Text>("textMyLevel");
        ShowLevel();

        textOtherCardCount      = FindWidget <Text>("textOtherCardCount");
        textOtherCardCount.text = "0";

        textOtherLevel      = FindWidget <Text>("textOtherLevel");
        textOtherAvatarname = FindWidget <Text>("textOtherAvatarname");

        RegisterReturnButton();

        btnJoinRoom = FindWidget <Button>("btnJoinRoom");
        btnJoinRoom.onClick.AddListener(() =>
        {
            WebSocketConnector.Instance.OnRemoteCall("joinRoom", "老牛赶大车");
        });

        cardcell = FindWidget <Transform>("cardcell");
        cardcell.gameObject.SetActive(false);

        otherinfo = FindWidget <Transform>("otherinfo");
        otherinfo.gameObject.SetActive(false);
        URemoteData.ListeningParam(InfoNameDefs.AvatarLevel, ShowLevel);

        UICommonWait.Show();
        WebSocketConnector.Instance.OnInit(Utils.WebSocketURL + UILogin.CachedUsernameAndTokenArguments, evt =>
        {
            UICommonWait.Hide();
        }, msgEvt =>
        {
        }, errEvt =>
        {
        }, closeEvt =>
        {
        });

        WebSocketConnector.Instance.OnRegisterResponse("enter", OnJoinRoomCB);
        WebSocketConnector.Instance.OnRegisterResponse("cardsync", OnCardsSync);
        WebSocketConnector.Instance.OnRegisterResponse("result", OnResult);
        WebSocketConnector.Instance.OnRegisterResponse("dismissed", OnDismissed);
    }
コード例 #14
0
 //private static object lockObj = new object();
 private void DoRequest(string url, string scontent, Action <Newtonsoft.Json.Linq.JObject> actionResult, Action <string> actionFailed, bool bSortArguments, bool bShowCommonWait)
 {
     if (bShowCommonWait)
     {
         UICommonWait.Show();
     }
     new Thread(new ThreadStart(() =>
     {
         try
         {
             var uri = $"{url}?{scontent}";
             Debug.Log($"request {uri}");
             var req    = (HttpWebRequest)HttpWebRequest.Create(uri);
             req.Method = "GET";
             using (WebResponse wr = req.GetResponse())
             {
                 var result = new StreamReader(wr.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                 if (string.IsNullOrEmpty(result))
                 {
                     UIAlert.Show($"{url}请求返回空。");
                     return;
                 }
                 Debug.Log($"url {uri} result {result}");
                 //lock (lockObj)
                 {
                     callbacksProducing.Add(() =>
                     {
                         actionResult(JsonConvert.DeserializeObject(result) as JObject);
                     });
                 }
             }
         }
         catch (WebException ex)
         {
             Debug.Log($"web sender WebException:{ex.Message}\r\n{ex.StackTrace}");
             //lock (lockObj)
             {
                 callbacksProducing.Add(() =>
                 {
                     UIAlert.Show($"请求失败:{url} Web崩溃信息:{ex.Message}");
                     if (actionFailed != null)
                     {
                         actionFailed(ex.Message);
                     }
                 });
             }
         }
         catch (Exception ex)
         {
             Debug.Log($"web sender error:{ex.Message}\r\n{ex.StackTrace}");
             //lock (lockObj)
             {
                 callbacksProducing.Add(() =>
                 {
                     UIAlert.Show($"请求失败:{url} 崩溃信息:{ex.Message}");
                     if (actionFailed != null)
                     {
                         actionFailed(ex.Message);
                     }
                 });
             }
         }
     }
                                )).Start();
 }
コード例 #15
0
 //private static object lockObj = new object();
 private void DoGet(string url, Action <Newtonsoft.Json.Linq.JObject> actionResult, Action <string> actionFailed)
 {
     UICommonWait.Show();
     new Thread(new ThreadStart(() =>
     {
         try
         {
             var req    = (HttpWebRequest)HttpWebRequest.Create(url);
             req.Method = "GET";
             using (WebResponse wr = req.GetResponse())
             {
                 var result = new StreamReader(wr.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                 if (string.IsNullOrEmpty(result))
                 {
                     UIAlert.Show($"{url}请求返回空。");
                     return;
                 }
                 Debug.Log($"url {url} result {result}");
                 //lock (lockObj)
                 {
                     callbacksProducing.Add(() =>
                     {
                         //result = FilterResult(result);
                         Debug.Log($"url {url} FilterResult {result.URLDecode()}");
                         var jres = JsonConvert.DeserializeObject(result) as Newtonsoft.Json.Linq.JObject;
                         actionResult?.Invoke(jres);
                     });
                 }
             }
         }
         catch (WebException ex)
         {
             Debug.Log($"web sender WebException:{ex.Message}\r\n{ex.StackTrace}");
             //lock (lockObj)
             {
                 callbacksProducing.Add(() =>
                 {
                     UIAlert.Show($"请求失败:{url} Web崩溃信息:{ex.Message}");
                     if (actionFailed != null)
                     {
                         actionFailed(ex.Message);
                     }
                 });
             }
         }
         catch (Exception ex)
         {
             Debug.Log($"web sender error:{ex.Message}\r\n{ex.StackTrace}");
             //lock (lockObj)
             {
                 callbacksProducing.Add(() =>
                 {
                     UIAlert.Show($"请求失败:{url} 崩溃信息:{ex.Message}");
                     if (actionFailed != null)
                     {
                         actionFailed(ex.Message);
                     }
                 });
             }
         }
     }
                                )).Start();
 }
コード例 #16
0
    protected override void InitComponents()
    {
        UICommonWait.Show();
        Task.Run(async() =>
        {
            if (!LibClient.AClientApp.bConnected)
            {
                await LibClient.AClientApp.StartClient();
            }
            UWebSender.Instance.AddProducingAction(() =>
            {
                UICommonWait.Hide();
            });
        });

        inputUsername = FindWidget <InputField>("inputUsername");
        if (!string.IsNullOrEmpty(CachedUsername))
        {
            inputUsername.text = CachedUsername;
        }

        inputPassword = FindWidget <InputField>("inputPassword");
        var btnLogin = FindWidget <Button>("btnLogin");

        btnLogin.onClick.AddListener(() =>
        {
            Task.Run(async() =>
            {
                if (!LibClient.AClientApp.bConnected)
                {
                    await LibClient.AClientApp.StartClient();
                }

                if (!LibClient.AClientApp.bConnected)
                {
                    AOutput.Log($"连接失败!");
                    return;
                }

                UWebSender.Instance.AddProducingAction(OnLogin);
            });
            btnLogin.enabled = false;
        });
        var btnRegister = FindWidget <Button>("btnRegister");

        btnRegister.onClick.AddListener(() =>
        {
            OnUnloadThis();

            LoadAnotherUI <UIRegister>();
        });

        if (!string.IsNullOrEmpty(token))
        {
            inputPassword.text = "******";
            btnLogin.enabled   = false;
            UStaticWebRequests.OnWebRequest("Login/CheckToken", "username="******"&token=" + token, jobj =>
            {
                OnSelectAvatar();
            },
                                            jfail =>
            {
                btnLogin.enabled   = true;
                inputPassword.text = "";
                token = "";
            });
        }
    }