コード例 #1
0
        HandleResult OnReceive(IntPtr connId, byte[] bytes)
        {
            log.Info($"收到消息,connId:{connId}");
            try
            {
                ReceiveObj obj = new ReceiveObj(connId, bytes);
                new System.Threading.Tasks.Task(() => { doAskCilentReq(obj); }).Start();
//                new Thread(() => { doAskCilentReq(obj); }).Start();
            }
            catch (Exception e)
            {
                log.Error(e.Message);
            }
            return(HandleResult.Ok);
        }
コード例 #2
0
        // 处理客户端的请求
        void doAskCilentReq(object obj)
        {
            // 模拟耗时操作,比如数据库操作,IO操作
            // Thread.Sleep(3000);

            ReceiveObj receiveObj = (ReceiveObj)obj;
            string     text       = Encoding.UTF8.GetString(receiveObj.m_bytes, 0, receiveObj.m_bytes.Length);

            log.Info("收到消息:" + text);

            JObject jo;

            try
            {
                jo = JObject.Parse(text);
            }
            catch (JsonReaderException ex)
            {
                // 传过来的数据不是json格式的,一律不理
                log.Warn("客户端传来非json数据:" + text);
                return;
            }
            JToken tag;
            bool   isSuccess = jo.TryGetValue("tag", out tag);

            if (!isSuccess)
            {
                log.Warn("传入的值没有TAG");
                return;
            }

            if (tag.ToString().Equals(Consts.Tag_Login))
            {
                if (!serviceDic.ContainsKey(TljServiceType.LoginService))
                {
                    serviceDic.Add(TljServiceType.LoginService, receiveObj.m_connId);
                }
            }
            else if (tag.ToString().Equals(Consts.Tag_GetAIList))
            {
                if (!serviceDic.ContainsKey(TljServiceType.LoginService))
                {
                    serviceDic.Add(TljServiceType.PlayService, receiveObj.m_connId);
                }
            }
            else if (tag.ToString().Equals(Consts.Tag_GetTurntable))
            {
                if (!serviceDic.ContainsKey(TljServiceType.LoginService))
                {
                    serviceDic.Add(TljServiceType.LogicService, receiveObj.m_connId);
                }
            }

            BaseHandler baseHandler;

            if (handlerDic.TryGetValue(tag.ToString(), out baseHandler))
            {
                try
                {
                    var onRequest = baseHandler.OnResponse(text);
                    if (onRequest != null)
                    {
                        sendMessage(receiveObj.m_connId, onRequest);
                    }
                }
                catch (Exception e)
                {
                    log.Warn("处理回调数据错误:" + e);
                }
            }
            else
            {
                log.Warn("没有得到tag对应的handler:" + tag);
            }
        }