EventSource JS实时通信

前台 html

  

后台 asp.net  mvc

        public void GetNotices(string user)
        {
            var sb = new System.Text.StringBuilder();
            sb.AppendFormat("retry:{0}\n\n", 2000);      //推送间隔
            sb.AppendFormat("event:{0}\n\n", "message"); //绑定onmessage
            sb.AppendFormat("id:{0}\n\n", System.Threading.Thread.CurrentThread.ManagedThreadId);
            string info = "当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId + "  时间:" + DateTime.Now.ToString()+" 用户:"+ user;
            sb.AppendFormat("data:\"{0}\"\n\n", info);

            Response.ContentType = "text/event-stream";
            Response.Expires = -1;
            Response.Write(sb.ToString());
            Response.Flush();
        }

允许跨域配置

节点中添加

   
      
        
        
        
      
    

 

你可能感兴趣的:(C#)