简体   繁体   English

无法在OnDisconnected上调用客户端方法

[英]Not able to call client method on OnDisconnected

I've a disconnect method in TestHub(which is inherited by Hub) class for signal R. I'm not able to call javascript method fnDeleteCustomer from OnDisconnected method, however same same js method gets called on Connect method. 我在TestHub(由Hub继承)类中有一个断开连接的方法,用于信号R。我无法从OnDisconnected方法调用javascript方法fnDeleteCustomer ,但是在Connect方法上调用了相同的js方法。 What I'm doing wrong? 我做错了什么?

 public override Task OnDisconnected()
        {
            try
            {
                 var customer = ConnectedUsers.Find(x => x.ConnectionID == Context.ConnectionId);
                if (customer!=null)
                {
                  Clients.Client(customer.ConnectionID).fnDeleteCustomer(customer.UserId);
                  return base.OnDisconnected();
                }
            }
            catch { };
            return null;
        }

According to MSDN : 根据MSDN

Occurs when a connection disconnects from this hub instance. 当连接与此集线器实例断开连接时发生。

So you have no any alive connection and you cannot access the client hub and its methods. 因此,您没有任何活动连接,并且无法访问客户端中心及其方法。

I suppose you should use client-side disconnected event: 我想您应该使用客户端断开连接事件:

$.connection.hub.disconnected(function() {
    $.connection.hub.fnDeleteCustomer(userId);
});

More information about signalr lifetime events can be found here . 有关信号器寿命事件的更多信息,请参见此处

You are not able to execute fnDeleteCustomer because at the moment of executing OnDisconnected , the client had already disconnected from the hub, so in that moment, the client wil not have a ConnectionId . 您无法执行fnDeleteCustomer因为在执行OnDisconnected ,客户端已经从集线器断开连接,因此在那时,客户端将没有ConnectionId

Sure, you can use the client disconnected method, but the SignalR disconnection mostly happens when the client leaves the respective page. 当然,您可以使用客户端disconnected方法,但是SignalR断开连接通常在客户端离开相应页面时发生。

From my point of view, it is not the client who just left that you want to execute the fnDeleteCustomer method, but the remaining ones so they can be notified that somebody left. 从我的角度来看,您不是要执行fnDeleteCustomer方法的客户端,而是其余的,以便可以通知有人离开了。

Hope this helps! 希望这可以帮助! Best of luck! 祝你好运!

EDIT: 编辑:

If you want to notify all the other clients that someone left, you simply do so: 如果您想通知其他所有客户某人离开了,只需这样做:

    public override OnDisconnected()
    {
        var customer = ConnectedUsers.Find(x => x.ConnectionID == Context.ConnectionId);
        Clients.All.notifySomeoneLeft(customer.Name);
    }

Then, you create your client method notifySomeoneLeft : 然后,创建客户端方法notifySomeoneLeft

$.connection.client.notifySomeoneLeft = function(customerName){
   alert(customerName + "just left!");
  };

And that's it. 就是这样。 Everytime someone leaves, all the connected clients will get an alert. 每当有人离开时,所有连接的客户端都会收到警报。

Best of luck! 祝你好运!

public override OnDisconnected()
    {
        var customer = ConnectedUsers.Find(x => x.ConnectionID == Context.ConnectionId);
        Clients.All.notifySomeoneLeft(customer.Name);
    }

The context.connectionId is getting new connectionId instead of old one. context.connectionId正在获取新的connectionId,而不是旧的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM