• Asp.net MVC Comet推送


    一、简介

      在Asp.net MVC实现的Comet推送的原理很简单。

      服务器端:接收到服务器发送的AJAX请求,服务器端并不返回,而是将其Hold住,待到有东西要通知客户端时,才将这个请求返回。

      客户端:请求异步Action,当接收到一个返回时,立即又再发送一个。

      缺点:会长期占用一个Asp.net处理线程。但相比于轮询,其节省了带宽。

      示例:

      新建一个Controller如下:

    复制代码
        //Comet服务器推送控制器(需设置NoAsyncTimeout,防止长时间请求挂起超时错误)
        [NoAsyncTimeout, SessionState(SessionStateBehavior.ReadOnly)]
        public class CometController : AsyncController   //需要继承自异步的AsyncController
        {
            /// <summary>
            /// 异步方法,处理客户端发起的请求
            /// </summary>
            public void IndexAsync()
            {
                AsyncManager.OutstandingOperations.Increment();
                AsyncManager.Parameters["info"] = "怎么了";
                AsyncManager.OutstandingOperations.Decrement();
            }
    
            /// <summary>
            /// 当异步线程完成时向客户端发送响应
            /// </summary>
            /// <param name="token">数据封装对象</param>
            /// <returns></returns>
            public ActionResult IndexCompleted(string info)
            {
                return Json(info, JsonRequestBehavior.AllowGet);
            }
        }
    复制代码

      随便找一个页面,通过AJAX请求这一个异步Action:

    复制代码
    <html>
    <head>
        <title>AJAX测试</title>
        <script src="/Content/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                getCometServerPush();
            })
    
            function getCometServerPush() {
                $.ajax({
                    cache: false,
                    url: '/Comet/Index',
                    success: function (data) {
                        $("#info").html(data);
                        getCometServerPush();
                    }
                });
            }
    
        </script>
    </head>
    <body>
        <div id="info"></div>
    </body>
    </html>
    复制代码

      上面的示例,如果你在Action上下一个断点,会不停的看到断点在循环。说明异步客户端不停地在推送。当然这个示例仅仅是说明推送的原理。

    二、应用

      应用:监控服务器上的一个txt文件,当有变化时,推送内容到客户端。

    复制代码
        //Comet服务器推送控制器(需设置NoAsyncTimeout,防止长时间请求挂起超时错误)
        [NoAsyncTimeout, SessionState(SessionStateBehavior.ReadOnly)]
        public class CometController : AsyncController   //需要继承自异步的AsyncController
        {
            /// <summary>
            /// 异步方法,处理客户端发起的请求
            /// </summary>
            public void IndexAsync()
            {
                AsyncManager.OutstandingOperations.Increment();
    
                FileSystemWatcher FSW = new FileSystemWatcher();
                FSW.Filter = "123.txt";              //仅仅监控123.txt文件
                FSW.Path = Server.MapPath(@"/");   //设置监控路径
                FSW.EnableRaisingEvents = true;  //启动监控
                //FileSystemWatcher暂时有个多次触发的问题,但与本推送示例无关,故不解决
                FSW.Changed += (object source, FileSystemEventArgs e) =>
                {
                    AsyncManager.Parameters["info"] = System.IO.File.ReadAllText(Server.MapPath(@"/123.txt"),System.Text.Encoding.Default); ;
                    AsyncManager.OutstandingOperations.Decrement();
                };
            }
    
            /// <summary>
            /// 当异步线程完成时向客户端发送响应
            /// </summary>
            /// <param name="token">数据封装对象</param>
            /// <returns></returns>
            public ActionResult IndexCompleted(string info)
            {
                return Json(info, JsonRequestBehavior.AllowGet);
            }
        }
    复制代码

      更多流逼的功能,留着读者自由发挥。

  • 相关阅读:
    ubuntu16下点亮C170摄像头的一波三折
    看完这张图,开源协议门清
    Qt调试信息重定向输出(qInstallMessageHandler)
    C++专业术语
    vim 复制 单个 单词: 移动光标到单词词首,快速摁 yw
    讲真的
    bcp文件, 逗号文件
    缩写: i = i + 1 和 i += 1,可以看做是 i 自加的值 是1。
    $identify 的 “identify” 表示一个Perl标识符,即 identifier
    第八章: 以正则表达式进行匹配
  • 原文地址:https://www.cnblogs.com/sylone/p/6094413.html
Copyright © 2020-2023  润新知