Example #1
0
    public void OnLogin()
    {
        if (m_TcpClient.IsConnected)
        {
            reqLogin req = new reqLogin();
            req.Username = "******";


            m_TcpClient.Send(req);
        }
    }
Example #2
0
        public void reconnect(Action <bool, Action> completion)
        {
            if (ConnectionManager.sharedInstance.connectionNamed(Config.CONNECTION_NAME_MATCHMAKER) != null)
            {
                Dispatcher.main.dispatchAsync(() => {
                    completion(true, () => {
                    });
                }, 1.0f);
                return;
            }

            if (_currentServerIndex == _serverAddressList.Count)
            {
                Dispatcher.main.dispatchAsync(() => {
                    completion(false, () => {
                        _currentServerIndex = 0;
                    });
                }, 1.0f);
                return;
            }
            var address = _serverAddressList [_currentServerIndex].Key;
            var port    = _serverAddressList [_currentServerIndex].Value;

            if (Config.isOverseas)
            {
                address = "game.baidu.com";
                port    = 3000;
            }

            ConnectionManager.sharedInstance.reconnect(Config.CONNECTION_NAME_MATCHMAKER, address, port, (status, connection) => {
                if (status == ConnectionStatus.Broken)
                {
                    ++_currentServerIndex;
                    reconnect(completion);
                }
                else if (status == ConnectionStatus.Connected)
                {
                    isConnected         = true;
                    _currentServerIndex = 0;

                    connection.addMessageHandler((uint)MessageCategory.Ack | (uint)MatchMakerMessageID.NtcTabUser, onPlayerInGameChanged);
                    connection.addMessageHandler((uint)MessageCategory.Ack | (uint)MatchMakerMessageID.StartGame, onStartGame);
                    connection.addMessageHandler((uint)MessageCategory.Ack | (uint)MatchMakerMessageID.GameList, onGameListUpdated);
                    connection.addMessageHandler((uint)MessageCategory.Ack | (uint)MatchMakerMessageID.NtcMsg, onMessage);
                    connection.addMessageHandler((uint)MessageCategory.Ack | (uint)MatchMakerMessageID.NtcInvite, onInvitationArrived);
                    connection.addMessageHandler((uint)MessageCategory.Ack | (uint)MatchMakerMessageID.NtcDisbandTab, onDisbandTab);
                    serverConnected.fire(EventArgs.Empty, () => {
                        var info       = Account.localPlayer;
                        var data       = new reqLogin();
                        data.nickName  = Encoding.UTF8.GetBytes(Account.localPlayer.nickName);
                        data.avatarUrl = Encoding.UTF8.GetBytes(Account.localPlayer.avatarUrl);
                        data.channelId = GameConfig.global.loginStatusData.channelId;
                        data.gameId    = GameConfig.global.loginStatusData.gameId;
                        data.ticket    = GameConfig.global.loginStatusData.ticket;

                        var request = new Request <OurgameHeader> ((uint)MatchMakerMessageID.LOGIN);
                        request.setData(data);

                        request.responseMessageId = (uint)MessageCategory.Ack | (uint)MatchMakerMessageID.LOGIN;
                        request.responseCallback  = (response, responseCompletion) => {
                            var ack = response.read <ackLogin> ();

                            bool isContinueGame = false;
                            if (ack.result == 0)
                            {
                                Account.localPlayer.setUserData(ack);

                                if (ack.enterCode != null)
                                {
                                    GameConfig.global.enterCode = Encoding.UTF8.GetString(ack.enterCode);
                                    isContinueGame = true;
                                }
                            }

                            completion(ack.result == 0, () => {
                                if (isContinueGame)
                                {
                                    continueGame.fire(EventArgs.Empty, responseCompletion);
                                }
                                else
                                {
                                    responseCompletion();
                                }
                            });
                        };

                        connection.sendRequest(request);
                    });
                }
            });
        }