示例#1
0
 void Update()
 {
     if (Time.time > m_NextPingSentTime)
     {
         m_Ping.PingSent(Time.time);
         m_ClientSession.Send(BitConverter.GetBytes(Time.time));
         m_NextPingSentTime = Time.time + 1f;
     }
     if (m_ClientSession.GetRecvedData(m_ClientRecvedData))
     {
         while (m_ClientRecvedData.Count != 0)
         {
             var data = m_ClientRecvedData.Dequeue();
             m_Ping.PingBack(BitConverter.ToSingle(data, 0));
         }
     }
     if (m_ServerSession.GetRecvedData(m_ServerRecvedData))
     {
         while (m_ServerRecvedData.Count != 0)
         {
             var data = m_ServerRecvedData.Dequeue();
             StartCoroutine(DelaySend(data, UnityEngine.Random.Range(0.1f, 0.3f)));
         }
     }
 }
示例#2
0
 void Update()
 {
     if (m_ServerSession.GetRecvedData(m_RecvedData))
     {
         while (m_RecvedData.Count != 0)
         {
             var data = m_RecvedData.Dequeue();
             ColoredLogger.Log("Msg From User: [" + Encoding.ASCII.GetString(data) + "]", ColoredLogger.LogColor.Yellow);
         }
     }
 }
示例#3
0
 void Update()
 {
     if (m_ServerSession.GetRecvedData(m_RecvedData))
     {
         while (m_RecvedData.Count != 0)
         {
             var       data = XOR(m_RecvedData.Dequeue(), KEY);
             MoveToMsg msg  = new MoveToMsg();
             msg.Unserialize(data);
             ColoredLogger.Log(
                 "Msg From User: [" +
                 string.Format("PlayerID={0},TargetPosition={1},Speed={2}", msg.PlayerID, msg.TargetPosition.ToString(), msg.Speed) +
                 "]", ColoredLogger.LogColor.Yellow);
         }
     }
 }
示例#4
0
    void ServerUpdate()
    {
        if (m_HasClientConnected == false)
        {
            if (m_ServerSession.GetRecvedData(m_ServerRecvedData))
            {
                while (m_ServerRecvedData.Count != 0)
                {
                    m_HasClientConnected = true;
                    m_ServerRecvedData.Dequeue();
                }
            }
        }
        else
        {
            Vector3 toTarget = m_ServerTargetPos - m_ServerPos;
            if (toTarget.sqrMagnitude < 0.5f)
            {
                m_ServerTargetPos = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
            }
            else
            {
                toTarget.Normalize();
                m_ServerPos += 6 * Time.deltaTime * toTarget;
                m_ServerRot  = Quaternion.LookRotation(toTarget);
            }
            if (Time.time > m_NextSyncTime)
            {
                StateMsg msg = new StateMsg();
                msg.TargetPosition    = m_ServerPos;
                msg.TargetOrientation = m_ServerRot;
                msg.TimeStamp         = Time.time;

                m_ServerSession.Send(msg.Serialize());
                m_NextSyncTime = Time.time + 0.2f;
            }
            ServerObjectTF.position = m_ServerPos;
            ServerObjectTF.rotation = m_ServerRot;
        }
    }