.net 模拟推送消息

建立一份新的aspx页面,在aspx.cs里的page_load里加上如下代码...然后保存,访问一下...就可以看到服务器推送模式的效果了...

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "multipart/x-mixed-replace;boundary=--TempString--";
            HttpContext.Current.Response.StatusCode = 200;
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.Write("--TempString--\r\n");
            HttpContext.Current.Response.Flush();
            while (HttpContext.Current.Response.IsClientConnected)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Write("date : " + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\r\n");
                HttpContext.Current.Response.Write("--TempString--\r\n");
                HttpContext.Current.Response.Flush();
                Thread.Sleep(1000);
            }
        }

你可能感兴趣的:(.net)