你的位置:首页 > 软件开发 > 网页设计 > [ASP.NET MVC] Real

[ASP.NET MVC] Real

发布时间:2016-04-18 12:00:05
最近有时间,打算看看SignalR,顺便了解一下Server Sent Events。 Controller输出的数据格式为:data:[数据]\n\n。输出的数据尝试8000多字符也没问题,具体的上限就没试了。但是如果数据里也包含\n\n的话,数据就会被截断。 1 publ ...

[ASP.NET MVC] Real

最近有时间,打算看看SignalR,顺便了解一下Server Sent Events。

 

Controller

输出的数据格式为:data:[数据]\n\n。输出的数据尝试8000多字符也没问题,具体的上限就没试了。但是如果数据里也包含\n\n的话,数据就会被截断。

 1 public class HomeController : Controller 2 { 3   // GET: Home 4   public ActionResult Index() 5   { 6     return View(); 7   } 8  9   //Server-Sent Event10   public void Sse()11   {12     Random random = new Random();13 14     //设置HTTP MIME 类型,不缓存页面15     Response.ContentType = "text/event-stream";16     Response.CacheControl = "no-cache";17       18     while (Response.IsClientConnected)19     {20       try21       {22         string data = SseData(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " from http://www.cnblogs.com/ainijiutian/");23         Response.Write(data);24         Response.Flush();25 26         System.Threading.Thread.Sleep(random.Next(500, 5000));27       }28       catch (Exception ex)29       {30         System.Diagnostics.Debug.WriteLine(ex.Message);31       }32     };33       34     Response.End();35     Response.Close();36   }37 38   //SSE data39   private string SseData(string data)40   {41     return "data:" + data + "\n\n";42   }43 }

原标题:[ASP.NET MVC] Real

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。