长轮询停止其它请求1或2分钟 [英] Long polling Stop other request for 1 or 2 minutes

查看:292
本文介绍了长轮询停止其它请求1或2分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建一个聊天系统,我使用了长寿命的要求得到消息,并使用一个jQuery请求发送消息是这样的:

During create a chat system , I use a long life request to get message , and use a jquery request to send message like this :

*的发送:*

$("#btn").click(function () { 
    $.ajax({
        type: "POST",
        url: "Chat.aspx/Insert",
        data: "{ 'Str' :'" + $("#txtStr").val() + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
        },
        error: function () {

        }
    });
});

收件人:

function Refresh() {

    $.ajax({
        type: "POST",
        url: "Chat.aspx/GetRecords",
        data: "{ 'Id' : " + $("#hdnV1").val() + "}",
        success: function (data) {

            $.each($(data.d), function () {

               //Show it to user

            });

        },
        complete: function () {
            return Refresh();
        },

        contentType: "application/json; charset=utf-8",
        dataType: "json",
        traditional: true,
        async: true
    });

}

这是我的服务器端code得到的消息:

and this is my server side code to get messages :

[WebMethod]
public static object GetRecords(int Id)
{
    TestEntities db = new TestEntities();
    int count = 0;
    while (true)
    {
        if (count++ >= 300)
            return null;


        System.Threading.Thread.Sleep(1000);
        var list = db.Commets.Where(rec => rec.Id > Id).Select(rec => new { Id = rec.Id, Str = rec.Str }).ToList();

        if (list.Count > 0)
            return list;

    }
}

当用户写的东西,点击发送按钮,请求,因为长寿命的要求正在执行进入待定状态,我的事情

When user writes something and clicks on the Send button , request goes to pending state , and I thing because the long life request is executing

我核对一下在萤火虫,有没有人在那里帮助我!?!
欲了解更多详情评论我,请和我的英语不好对不起语法,我的英语新

I check them up in firebug, is there anybody out there to help me !?! For more details comment me please , and sorry about my bad English syntax , I am new in English

感谢

推荐答案

这是不是建立在asp.net聊天的最好方法。随着用户数量的增加这种方法不会缩放

This is not the best method to build a chat in asp.net. As the number of users increases this method won't scale.

看看 SignalR 是建立一个聊天应用程序一个不错的选择。

Take a look at SignalR is a good option to build a chat application.

不过,如果你想自己做出于某种原因,打造asp.net聊天最有效的方法是使用的 IHttpAsyncHandler 和Ajax请求。

However if you do want to do it yourself for some reason, the most efficient method to build a chat in asp.net is to use a IHttpAsyncHandler and ajax requests.

Asp.net具有工作线程的数量有限。把其中一人睡觉会杀了你的应用,即使是相对较小的用户。

Asp.net has a limited number of worker threads. Putting one of them to sleep will just kill your application even for a relatively small number of users.

这是异步请求,允许您延迟,直到发生外部事件请求的响应。结果
一个用户打电话到该处理程序并等待,直到有人给他发个消息。结果
消息只要您将它们发送给用户交付。

An async request allows you to delay the response of a request till an external event occurs.
A user makes a call to this handler and waits till someone sends him a message.
Messages are delivered as soon as you send them to a user.

在接收消息的客户端发出另一个请求并等待下一个信息。
这是你如何保持一个持久连接开放,允许服务器将数据推到客户端。

On receiving a message the client makes another request and waits for the next message. This is how you keep a persistent connection open and allows the server to push data to the client.

这是一个很多比投票站现场检查邮件是否已经到达更有效。结果
使用异步处理器也保证了在用户等待的消息来没有asp.net线程被浪费了。具有定时轮询你会很快运行的线程走出来处理asp.net请求。

This is a lot more efficient than polling the site to check if messages have arrived.
Using the async handler also ensures that no asp.net threads are wasted while a user waits for messages to come. Polling with a timer you will quickly run out of threads to process requests in asp.net.

这可以确保您的聊天甚至可以作为网站的用户数量以及规模上升。

This ensures that your chat can scale well even as the number of users of the site goes up.

下面是一个完全的工作项目,实现此,阿贾克斯一起。

Here is a completely working project that implements this, along with ajax.

避免使用一个数据库作为通信的用户之间的网站上的方法。如果你要记录的聊天,让所有的写操作异步调用。

Avoid using a database as the method of communication between users on your site. If you want to log the chats, make all the writes async calls.

这篇关于长轮询停止其它请求1或2分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆