Exemplo n.º 1
0
        /// <summary>
        /// 新消息
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMessage(WebSocketSharp.MessageEventArgs e)
        {
            try
            {
                LastRecvTime = DateTime.Now;
                if (e.IsText)
                {
                    switch (e.Data)
                    {
                    case "HeartBeat":
                        this.Send(Newtonsoft.Json.JsonConvert.SerializeObject(new WSResModel(ResCode.HeartBeatRes)));
                        return;

                    case "TaskIsExist":
                        if (HandlerTaskManager.ContainsTask(this.TaskId))
                        {
                            this.Send(Newtonsoft.Json.JsonConvert.SerializeObject(new WSResModel(ResCode.Wait)));
                        }
                        else
                        {
                            SendAndClose(new WSResModel(ResCode.Err, "任务不存在!"));
                        }
                        return;
                    }
                }
                BaseHandler handler = HandlerManager.CreateHandler(PathName);
                if (handler == null)
                {
                    SendAndClose(new WSResModel(ResCode.Err, string.Format("请求路径{0}未找到对应Handler", PathName)));
                    return;
                }
                handler.Path           = this.PathName;
                handler.TaskId         = this.TaskId;
                handler.ReqIsWebSocket = true;
                handler.InPara         = e.Data;
                //添加任务并执行
                if (HandlerTaskManager.AddTask(handler))
                {
                    ThreadPool.QueueUserWorkItem((state) =>
                    {
                        BaseHandler bh = state as BaseHandler;
                        try
                        {
                            bh.HandlerTask(bh.InPara);
                        }
                        catch (Exception ex)
                        {
                            bh.ReturnToClient(new WSResModel(ResCode.Err, ex.Message));
                        }
                    }, handler);
                }
            }
            catch (Exception ex)
            {
                CommonInfo.Output("WS处理消息异常:{0}", ex.Message);
                SendAndClose(new WSResModel(ResCode.Err, string.Format("服务器WS处理消息异常", ex.Message)));
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Http请求处理
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void _httpServer_Handler(object sender, HttpRequestEventArgs e)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(e.Request.Url.AbsolutePath))
         {
             HttpResponse(e, new WSResModel(ResCode.Err, "请求路径不存在!"));
             return;
         }
         string path      = e.Request.Url.AbsolutePath.TrimStart('/');
         string taskId    = e.Request.QueryString["taskId"];
         string clientVer = e.Request.QueryString["clientVer"];//客户端版本号
         if (path == "TestConnect")
         {
             HttpResponse(e, new WSResModel(ResCode.OK));
             return;
         }
         if (!string.IsNullOrWhiteSpace(clientVer) && CommonInfo.Version != clientVer)
         {
             HttpResponse(e, new WSResModel(ResCode.ClientAutoUpgrade, string.Format("请将客户端版本升级至{0}", clientVer)));
             Program.ExitAndStartAutoUpgrade();
             return;
         }
         if (string.IsNullOrWhiteSpace(taskId))
         {
             HttpResponse(e, new WSResModel(ResCode.Err, "请求参数TaskId必须!"));
             return;
         }
         if (path == "_getTaskReslut")
         {
             Stopwatch sw = new Stopwatch();
             sw.Start();
             GetTaskWaitReslut(taskId, e, sw);
         }
         else
         {
             BaseHandler handler = HandlerManager.CreateHandler(path);
             if (handler == null)
             {
                 HttpResponse(e, new WSResModel(ResCode.Err, string.Format("请求路径{0}未找到对应Handler", path)));
                 return;
             }
             //参数解析
             string msg = string.Empty;
             if (e.Request.ContentLength64 > 0)
             {
                 using (System.IO.StreamReader stream = new System.IO.StreamReader(e.Request.InputStream, Encoding.UTF8))
                 {
                     msg = stream.ReadToEnd();
                 }
             }
             //断开并返回客户端
             HttpResponse(e, new WSResModel(ResCode.Wait));
             //线程继续处理消息
             handler.Path           = path;
             handler.TaskId         = taskId;
             handler.ReqIsWebSocket = false;
             handler.InPara         = msg;
             if (HandlerTaskManager.AddTask(handler))
             {
                 handler.HandlerTask(msg);
             }
         }
     }
     catch (Exception ex)
     {
         HttpResponse(e, new WSResModel(ResCode.Err, string.Format("服务器异常:{0}", ex.Message)));
     }
 }