Пример #1
0
 void handleProtocol(int pid, string jsonData)
 {
     // 这个方法应该由主线程调用
     try
     {
         string pName = string.Format("socket_{0}", pid);
         SFBaseResponseMessage obj = null;
         if (pid == int.MaxValue)
         {
         } // __start__
         else if (pid == 0)
         {
             obj = JsonUtility.FromJson <SFResponseMsgSocketHeartbeat>(jsonData);
         }
         else if (pid == 1)
         {
             obj = JsonUtility.FromJson <SFResponseMsgUnitLogin>(jsonData);
         }
         else if (pid == 2)
         {
             obj = JsonUtility.FromJson <SFResponseMsgNotifyRemoteUsers>(jsonData);
         }
         else if (pid == 3)
         {
             obj = JsonUtility.FromJson <SFResponseMsgUnitSync>(jsonData);
         }
         else if (pid == 4)
         {
             obj = JsonUtility.FromJson <SFResponseMsgNotifyUnitStatus>(jsonData);
         }
         else if (pid == 5)
         {
             obj = JsonUtility.FromJson <SFResponseMsgNotifyNewUserJoin>(jsonData);
         }
         else if (pid == 6)
         {
             obj = JsonUtility.FromJson <SFResponseMsgHostRoom>(jsonData);
         }
         else if (pid == 7)
         {
             obj = JsonUtility.FromJson <SFResponseMsgJoinRoom>(jsonData);
         }
         else // __end__
         {
             SFUtils.logWarning("不能识别的协议号: {0}", pid);
         }
         if (obj != null)
         {
             dispatcher.dispatchEvent(pName, obj);
         }
     }
     catch (Exception e)
     {
         SFUtils.logWarning("解析协议失败: {0}\ndata: {1}\n错误信息:{2}\n出错方法:{3}", pid, jsonData, e.Message, e.StackTrace);
     }
 }
Пример #2
0
        public void update(float dt)
        {
            // 发送队列
            while (m_sendQueue.Count > 0)
            {
                SFBaseRequestMessage req = m_sendQueue.Dequeue();
                string data = JsonUtility.ToJson(req);
                m_client.sendData(data);
                if (req.pid != 0 && req.pid != 3)
                {
                    SFUtils.log("Sending message[{0}]: {1}", data.Length, data);
                }
            }

            // 接收队列
            while (m_recvQueue.Count > 0)
            {
                string data = m_recvQueue.Dequeue();
                SFBaseResponseMessage obj = null;
                try
                {
                    obj = JsonUtility.FromJson <SFBaseResponseMessage>(data);
                }
                catch
                {
                }
                if (obj != null)
                {
                    handleProtocol(obj.pid, data);
                    if (obj.pid != 0 && obj.pid != 3 && obj.pid != 4)
                    {
                        SFUtils.log("收到信息:协议号={0}\ndata={1}", obj.pid, data);
                    }
                }
                else
                {
                    SFUtils.logWarning("不能解析的信息格式:\n" + data);
                }
            }

            // 心跳包
            m_heartbeatTimer += dt;
            if (m_heartbeatTimer > SFCommonConf.instance.heatbeatInterval)
            {
                m_heartbeatTimer -= SFCommonConf.instance.heatbeatInterval;
                sendHeartbeat();
            }
        }