输入用户名和密码,点击选择服务器后:
LoginWindow.cs
void OnLoginSubmit(GameObject go)
{
#if UNITY_STANDALONE_WIN
if(string.IsNullOrEmpty(mLoginAccountInput.value))
return;
mLoginPassInput.value = "123";
#else
if(string.IsNullOrEmpty(mLoginAccountInput.value)||string.IsNullOrEmpty(mLoginPassInput.value))
return;
#endif
mWaitingParent.gameObject.SetActive(true);
LoginCtrl.Instance.Login(mLoginAccountInput.value, mLoginPassInput.value);
}
然后,LoginCtrl.cs
//登陆
public void Login(string account, string pass)
{ SelectServerData.Instance.SetServerInfo((int)SdkManager.Instance.GetPlatFrom(), account, pass);
NetworkManager.Instance.canReconnect = false;
NetworkManager.Instance.Close();
Debug.Log("deng lu dengluy");
NetworkManager.Instance.Init(JxBlGame.Instance.LoginServerAdress, 49996, NetworkManager.ServerType.LoginServer);
}
public void Update(float fDeltaTime) //这个方法会在Mono中Update方法被逐帧调用
{
if (m_Client != null) //连接成功后,就转到这里
{
DealWithMsg(); //开始处理消息
if (mRecvResult != null)
{
if (m_RecvOverCount > 200 && Time.time > m_RecvOverTime)
{
Debugger.LogError("recv data over 200, so close network.");
Close();
return;
}
++m_RecvOverCount;
if (mRecvResult.IsCompleted)
{
try
{
Int32 n32BytesRead = m_Client.GetStream().EndRead(mRecvResult); //返回本次读取的字节数
m_RecvPos += n32BytesRead; //总接收的字节数量
if (n32BytesRead == 0)
{
Debugger.LogError("can't recv data now, so close network 2.");
Close();
return;
}
}
catch (Exception exc)
{
Debugger.LogError(exc.ToString());
Close();
return;
}
OnDataReceived(null, null);
if (m_Client != null)
{
try
{
mRecvResult = m_Client.GetStream().BeginRead(m_RecvBuffer, m_RecvPos, m_RecvBuffer.Length - m_RecvPos, null, null);
m_RecvOverTime = Time.time + mRecvOverDelayTime;
m_RecvOverCount = 0;
}
catch (Exception exc)
{
Debugger.LogError(exc.ToString());
Close();
return;
}
}
}
}
if (m_Client != null && m_Client.Connected == false)
{
Debugger.LogError("client is close by system, so close it now.");
Close();
return;
}
}
else if (m_Connecting != null)//这个地方要关注一下
{
if (m_ConnectOverCount > 200 && Time.time > m_ConnectOverTime) //连接超时
{
Debugger.LogError("can't connect, so close network.");
m_Client = m_Connecting;
m_Connecting = null;
mConnectResult = null;
OnConnectError(m_Client, null);
return;
}
++m_ConnectOverCount;
if (mConnectResult.IsCompleted) //如果连接已经完成
{
m_Client = m_Connecting; //此处要关注一下
m_Connecting = null;
mConnectResult = null;
if (m_Client.Connected) //并且已连接
{
try
{
m_Client.NoDelay = true;
m_Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
m_Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
m_Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
mRecvResult = m_Client.GetStream().BeginRead(m_RecvBuffer, 0, m_RecvBuffer.Length, null, null);//开始读
m_RecvOverTime = Time.time + mRecvOverDelayTime;
m_RecvOverCount = 0;
OnConnected(m_Client, null); //连首次接成功后调用这个
}
catch (Exception exc)
{
Debugger.LogError(exc.ToString());
Close();
return;
}
}
else
{
OnConnectError(m_Client, null);
}
}
}
else
{
Connect();
}
}
public void OnConnected(object sender, EventArgs e)
{
switch (serverType)
{
case ServerType.BalanceServer:
{
CGLCtrl_GameLogic.Instance.BsOneClinetLogin();
}
break;
case ServerType.GateServer:
{
++m_n32ConnectTimes;
if (m_n32ConnectTimes > 1)
{
CGLCtrl_GameLogic.Instance.EmsgTocsAskReconnect();
}
else
{
CGLCtrl_GameLogic.Instance.GameLogin();
}
EventCenter.Broadcast(EGameEvent.eGameEvent_ConnectServerSuccess);
}
break;
case ServerType.LoginServer:
{
CGLCtrl_GameLogic.Instance.EmsgToLs_AskLogin(); //see here
}
break;
}
}
利用protobuf数据类进行打包封装:
FR:海涛高软(hunk Xu)