Exemple #1
0
        ///处理网路断开事件
        private void _onDisconnect(Message retMsg)
        {
            if (__reconnectCoroutine != null)
            {
                /// 在重连过程中,又发生了断开连接(因为整个登录过程包含多个步骤,这是完全有可能的)
                StopCoroutine(__reconnectCoroutine);
                __reconnectCoroutine = null;
            }

            Disconnect();

            var reason = (string)retMsg.jsonObj["reason"];

            DebugLogger.Log(eLogType.LT_NET, "<color=red>Connection down, reason: </color>" + reason);

            if (OnDisconnected != null)
            {
                OnDisconnected(reason);
            }

            __reconnectCoroutine = StartCoroutine(_doReconnect());
        }
Exemple #2
0
        protected IEnumerator _doGetGameSvrInfo(Action <Constants> callback)
        {
            bool    bIsDone = false;
            Message retMsg  = null;

            _connection.Connect(ServerIP, Port, retMsg_ =>
            {
                bIsDone = true;
                retMsg  = retMsg_;
            });

            yield return(new WaitUntil(() => bIsDone));

            if (retMsg.id != Connection.SYS_MSG_CONNECTED)
            {
                var reason = (string)retMsg.jsonObj["reason"];
                DebugLogger.Log(eLogType.LT_NET, reason);
                if (callback != null)
                {
                    callback(Constants.NETWORK_ERROR);
                }
            }

            bIsDone = false;
            _connection.Handshake(new JsonObject(), data => { bIsDone = true; });

            yield return(new WaitUntil(() => bIsDone));

            bIsDone = false;

            bIsDone = false;
            JsonObject msg = new JsonObject();

            msg["uid"] = 1;
            Message message = null;

            _connection.Request(RequestMsg.REQUEST_CONNECTOR, msg, retMsg_ =>
            {
                bIsDone = true;
                message = retMsg_;
                _connection.RemoveAllEventListeners();
            });

            yield return(new WaitUntil(() => bIsDone));

            bIsDone = false;

            Constants code = (Constants)Enum.Parse(typeof(Constants), message.jsonObj["code"].ToString());

            if (code == Constants.SUCCESS)
            {
                _gameSvrIP   = message.jsonObj["host"].ToString();
                _gameSvrPort = int.Parse(message.jsonObj["port"].ToString());

                DebugLogger.Log(eLogType.LT_NET, "Game server IP: " + _gameSvrIP + ", Port: " + _gameSvrPort);

                loginState = LoginState.SvrInfoGot;
            }
            else
            {
                loginState = LoginState.Validated;
            }

            if (callback != null)
            {
                callback(code);
            }
            _connection.Disconnect();
        }