管理信号器中的多个浏览器选项卡 [英] Manage multiple browser tabs in signalr

查看:65
本文介绍了管理信号器中的多个浏览器选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个像MVC 4中的facebook这样的聊天应用程序。



我正在连接到集线器如下



I am developing a chat application like facebook in MVC 4.

I am connecting to hub like following

var chat = $.connection.chatHub;

$.connection.transports.longPolling.supportsKeepAlive = function () {
    return false;
}
$.connection.hub.qs = "UserID=" + $('#hdfLoggedInUserID').val();
$.connection.hub.start().done(function () {
    //...
});
$.connection.hub.disconnected(function () {
    setTimeout(function () {
        $.connection.hub.start();
    }, 5000); // Restart connection after 5 seconds.
});





我的集线器类如下:





And my hub class is like following

public override Task OnConnected()
    {
        var userID = Context.QueryString["UserID"];
        if (userID != null)
        {
            //set user online in database and save connection_id
            RefreshOnlineUsers(uId);
        }
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        var userID = Context.QueryString["UserID"];
        if (userID != null)
        {
            //set user offline in database
            RefreshOnlineUsers(uId);
        }
        return base.OnDisconnected(stopCalled);
    }



当我只使用一个浏览器标签时工作正常。



OnConnected浏览器选项卡打开时调用,而浏览器选项卡关闭时调用OnDisconnected。因此,当我打开一个选项卡然后OnConnected调用和用户状态(在线)和数据库中的connection_id更新。



之后我在新选项卡中打开我的应用程序相同的浏览器然后再次OnConnected方法调用和用户的新connection_id和数据库中的状态(在线)更新。



之后当我关闭新的浏览器选项卡然后OnDiconnected调用和用户的状态更改为数据库中的脱机。但是用户仍然在以前的浏览器选项卡中在线。



由于此问题,此用户根据数据库离线,但它在线。



问:如何解决这个多浏览器标签问题?



谢谢。



我尝试了什么:




It's working fine when I am working with only one browser tab.

OnConnected calls when browser tab open and OnDisconnected calls when browser tab closed. So according to this when I open one tab then OnConnected calls and user's status(online) and connection_id update in database.

After that I open my application in new tab of same browser then OnConnected method calls again and user's new connection_id and status(online) update in database.

After that when I close new browser tab then OnDiconnected calls and user's status changes to offline in database. But user is still online in previous browser tab.

Due to this issue this user is offline according to database but it's online.

Q : How can I resolve this multiple browser tab issue?

Thanks.

What I have tried:

I am already done lot of R&D about it but can't find any proper solution.

推荐答案

.connection.chatHub;
.connection.chatHub;


.connection。 transports.longPolling.supportsKeepAlive = function (){
return ;
}
.connection.transports.longPolling.supportsKeepAlive = function () { return false; }


.connection.hub.qs = UserID = +
.connection.hub.qs = "UserID=" +


这篇关于管理信号器中的多个浏览器选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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