Exemplo n.º 1
0
        HttpStatusCode HttpServerCallback(HttpServerContext context)
        {
            Console.WriteLine("Request Path:{0},{1}", context.Path, Guid.NewGuid());

            context.ResponseHeader["content-type"] = "text/html;charset=utf8";

            //异步块输出
            if (context.Path == "/chunk")
            {
                context.BeginWrite();
                try
                {
                    context.Write("同步块输出<br />");
                    var i = 0;
                    while (i++ < 6)
                    {
                        Thread.Sleep(1000);
                        context.Write(DateTime.Now.ToString() + "<br />");
                    }
                }
                finally
                {
                    //必需确保调用
                    context.EndWrite();
                }
                return(HttpStatusCode.OK);
            }
            //模拟异步块输出
            else if (context.Path == "/async")
            {
                context.BeginWrite();
                ThreadPool.QueueUserWorkItem((object o) =>
                {
                    try
                    {
                        context.Write("异步块输出<br />");
                        var i = 0;
                        while (i++ < 6)
                        {
                            Thread.Sleep(1000);
                            context.Write(DateTime.Now.ToString() + "<br />");
                        }
                    }
                    finally
                    {
                        //必需确保调用
                        context.EndWrite();
                    }
                });
                return(HttpStatusCode.OK);
            }
            //内容输出
            else
            {
                //Thread.Sleep(300);
                context.Content = string.Format("Request Path:{0}", context.Path);
                return(HttpStatusCode.OK);
            }
        }
Exemplo n.º 2
0
        private HttpStatusCode General(Action action, byte actionType, HttpServerContext httpContext, bool bin)
        {
            if (bin)
            {
                httpContext.ResponseHeader["Content-Type"] = "application/octet-stream";
            }
            else
            {
                httpContext.ResponseHeader["Content-Type"] = "application/json";
            }
            httpContext.BeginWrite();
            //
            var queue = httpContext.QueryString["queue"] ?? "";
            var id    = httpContext.QueryString["requestid"] ?? "";

            //
            action.actionType = actionType;
            action.channel    = new HttpState(httpContext, bin);
            action.queue      = queue;
            action.id         = id;
            //
            Program.ActionProcessor.Push(action);
            //
            return(HttpStatusCode.OK);
        }
Exemplo n.º 3
0
        private HttpStatusCode Push(byte actionType, HttpServerContext httpContext, bool bin)
        {
            var body = httpContext.PostData;

            if (body == null || body.Length == 0)
            {
                httpContext.ResponseHeader["Content-Type"] = "text/html";
                httpContext.Content = "No body";
                return(HttpStatusCode.BadRequest);
            }
            else if (httpContext.RequestHeader["Content-Type"] != "application/octet-stream")
            {
                httpContext.ResponseHeader["Content-Type"] = "text/html";
                httpContext.Content = "Please set content-type to application/octet-stream";
                return(HttpStatusCode.BadRequest);
            }
            //
            if (bin)
            {
                httpContext.ResponseHeader["Content-Type"] = "application/octet-stream";
            }
            else
            {
                httpContext.ResponseHeader["Content-Type"] = "application/json";
            }
            httpContext.BeginWrite();
            //
            var queue = httpContext.QueryString["queue"] ?? "";
            var id    = httpContext.QueryString["requestid"] ?? "";
            //
            var action = new PushAction();

            action.actionType = actionType;
            action.channel    = new HttpState(httpContext, bin);
            action.queue      = queue;
            action.id         = id;
            action.item       = new DataItem()
            {
                body = body
            };
            //
            Program.ActionProcessor.Push(action);
            //
            return(HttpStatusCode.OK);
        }
Exemplo n.º 4
0
        private HttpStatusCode Home(HttpServerContext httpContext)
        {
            var queuename = httpContext.QueryString["queue"] ?? "";

            //
            httpContext.ResponseHeader["Content-Type"] = "text/html";
            httpContext.BeginWrite();
            //
            var action = new HomeAction();

            action.actionType = Action.HOME;
            action.channel    = new HttpState(httpContext, false);
            action.queue      = queuename;
            action.id         = "";
            //
            Program.ActionProcessor.Push(action);
            //
            return(System.Net.HttpStatusCode.OK);
        }