Ajax long polling not working correctly

孤人 提交于 2019-12-07 11:02:24

问题


I am developing a simple strangers chat application using long polling in MVC 2. Its works fine in my development machine if i am opening the application different browsers.. i mean if i loaded the application in IE and mozilla, it works fine

if i took the appliction in two tabs of a browser (eg:IE) , the long polling are not firing from both tabs.. I mean, there is a start button to start chat which fire long polling. I can see it calling actions while debugging.. And my problem is, When i clicked start button from tab one , it fire a ajax request (long polling ( this req wait at server till another reqst comes)).and then i click the start button in tab two, it does not fire the ajax request until the first request is returned from the server(after timeout).

why this is happening? I read like browser will block multiple ajax request..Is that a reason for that? ..It work fine if i am using different browsers.The problem only came if i took two tab in same browser


回答1:


I read like browser will block multiple ajax request.

Yes, if you use sessions, ASP.NET blocks concurrent requests from the same session. Quote from the documentation:

Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.

Also make sure that you have disabled caching. For example if you use jquery use the cache: false option when polling:

$.ajax({
    url: '/poll',
    cache: false,
    success: function(result) {
        // ...
    }
});


来源:https://stackoverflow.com/questions/6143774/ajax-long-polling-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!