本文介绍了使用异步控制器进行长轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用异步控制器在asp.net mvc 3中实现长轮询,但我的问题是我无法在轮询期间从页面导航或创建其他ajax请求。异步控制器对我来说是一个新话题,所以我的问题是它应该像这样工作还是我做错了什么?代码如下。



I'm trying to implement long polling in asp.net mvc 3 with asynchronous controllers, but my problem is that i'm unable to navigate from the page or create other ajax requests during the polling. Asynchronous controllers are a new topic to me so my questions are is it supposed to work like this or am i doing something wrong? The codes are below.

$(window).load(function () {
    setTimeout(function () {
        doLongPolling();
    }, 1000);
});

function doLongPolling() {
    $.post("/AsyncTest/LongPoll", null, function (data) {
        alert(data.msg);
        doLongPolling();
    });
}










public class AsyncTest : AsyncController
    {
        public void LongPollAsync()
        {
            AsyncManager.OutstandingOperations.Increment();
            Thread.Sleep(20000);
            AsyncManager.Parameters["msg"] = "polling done";
            AsyncManager.OutstandingOperations.Decrement();
        }

        public ActionResult LongPollCompleted(string msg)
        {
            return this.Json(new { msg = msg });
        }
    }

推荐答案











public class AsyncTest : AsyncController
    {
        public void LongPollAsync()
        {
            AsyncManager.OutstandingOperations.Increment();
            Thread.Sleep(20000);
            AsyncManager.Parameters["msg"] = "polling done";
            AsyncManager.OutstandingOperations.Decrement();
        }

        public ActionResult LongPollCompleted(string msg)
        {
            return this.Json(new { msg = msg });
        }
    }



这篇关于使用异步控制器进行长轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 06:31