SignalR分离事件多个集线器 [英] SignalR separated events for multiple hubs

查看:2025
本文介绍了SignalR分离事件多个集线器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一些SignalR集线器和他们一些独立OnDisconnected事件。

为了这个问题,有些网页上我有两个集线器:

  1. StatusHub:处理网站上的用户的状态。该中心的方法被称为网站的每一页上,并hadle用户连接和断开,因为他通过导航网站更新用户状态的逻辑。

  2. ChatHub:处理一切相关网站的实时聊天。该中心的方法只能从网站的特定页面,当特定的按钮,用户点击。

  3. 称为

我遇到的问题是,即使两个中心都实现了他们自己的版本的OnConnected和OnDisconnected方法,只有StatusHub方法被解雇时,ChatHub方法不被调用。

我不知道这是否是设计,还是我做错了什么。到目前为止,我读过一些文章和答案,指出连接到中心服务器是一个连接,而我没有连接到特定的枢纽,我连接到​​中心服务器作为一个整体所以只有一种方法将永远不会承认和调用。

任何人都可以与SignalR更多的经验告诉我,如果有一种方法可以分开两个集线器的连接和断开事件,或者,如果这是真正的意思是这样?

我把仅有数几张低于code,以更好地说明方案,但如果你需要查看更多code,只问我会添加它。

StatusHub.cs

 公共类StatusHub:集线器{
    公众覆盖任务OnDisconnected()
    {
        根据需要//处理断线事件
        返回base.OnDisconnected();
    }


    公众覆盖任务OnConnected()
    {
        根据需要//处理相关事件
        返回base.OnConnected();
    }
}
 

ChatHub.cs

 公共类ChatHub:集线器{
    公众覆盖任务OnDisconnected()
    {
        //应该处理聊天断开事件,但从未达到过
        返回base.OnDisconnected();
    }


    公众覆盖任务OnConnected()
    {
        //应该处理聊天连接事件,但从未达到过
        返回base.OnConnected();
    }
}
 

code启动中心服务器连接(在每一页上执行):

  $。connection.hub.start()。完成(功能(){
    //很多code在这里(无关的,我认为这个问题)
});
 

解决方案

你有没有解决呢?你描述的非常似乎是一个问题,不登记为 ChatHub 任何客户端的方法。当没有客户端的方法的注册时,客户端可以调用服务器上的一个轮毂的方法,并且将获得的结果(非常类似的无状态协议请求 - 响应性),但由于没有办法让服务器调用上的任何东西没有被要求提供响应(由客户端调用枢纽方法手段)客户端,它是由设计,这样的连接不触发 OnConnected OnReconnected OnDisconnected 过载。

解决的方法是相当简单的,它足以这样注册无操作客户端方式:

  $(函数()
{
  //空操作客户端的方法,以便在轮毂的
  // OnConnected,OnReconnected和OnDisconnected方法重载被触发。
  $ .connection.chatHub.noop =功能(){};
  $ .connection.hub.start()
    .done(函数(){执行console.log(SignalR连接的建立和运行。);})
    .fail(功能(错误){执行console.log(错误);});
});
 

我不知道进入此确切推理,但过载,即通常用于跟踪连接的客户端的,并有可能它们与抽象关联(某物SignalR组不能提供,因为它们不是可枚举)是相当没用的连接,你不能戳回来,所以这是我的两分钱,为什么它有许多工作要做这样的。

I'm trying to set up some SignalR Hubs and some separate OnDisconnected events for them.

For the sake of the question, on some page I have two Hubs:

  1. StatusHub: Handles the status of the user on the website. This hub's methods are called on every page of the website and hadle the logic of the user connection and disconnection to update the user status as he navigates through the WebSite.

  2. ChatHub: Handles everything related to the website real time chat. This hub's methods are only called from a specific page of the website, when the user click on specific buttons.

The problem I'm experiencing is that even though both of the hub classes implement their own version of the OnConnected and OnDisconnected methods, only the StatusHub methods are fired, the ChatHub methods aren't being called.

I don't know if that is by design, or if I'm doing something wrong. So far I've read some articles and answers that states that the connection to the hub server is a single connection and that I don't connect to a specific hub, I connect to the Hub server as a whole so only one of the methods will ever be recognized and called.

Can anyone with more experience with SignalR tell me if there is a way to separate the connection and disconnection events of the two hubs or if that's really meant to be this way?

I'll put just a few some pieces of code below to better illustrate the scenario but if you need to see more code, just ask and I'll add it.

StatusHub.cs

public class StatusHub : Hub { 
    public override Task OnDisconnected()
    {
        //handles the disconnected event as needed
        return base.OnDisconnected();
    }


    public override Task OnConnected()
    {
        //handles the connected event as needed
        return base.OnConnected();
    }
}

ChatHub.cs

public class ChatHub : Hub { 
    public override Task OnDisconnected()
    {
        //should handle the chat disconnected event but is never reached
        return base.OnDisconnected();
    }


    public override Task OnConnected()
    {
        //should handle the chat connected event but is never reached
        return base.OnConnected();
    }
}

Code that starts the hub server connection (executed on every page):

$.connection.hub.start().done(function() {
    //a lot of code here (irrelevant to the question I think)
});

解决方案

Have you resolved this? What you describe very much seems like an issue with not registering any client method for the ChatHub. When no client methods are registered, the client can invoke a hub method on the server, and will get the result (very similar to request-response nature of the stateless protocols), but since there is no way for the server to call anything on the client without being requested to provide response (by the means of client invoking the hub method), it is by design that such connections don't trigger the OnConnected, OnReconnected and OnDisconnected overloads.

The solution is fairly simple, it is enough to register a no-op client method like this:

$(function()
{
  // A no-op client method so the hub's
  // OnConnected, OnReconnected and OnDisconnected method overloads are triggered.
  $.connection.chatHub.noop = function () { };
  $.connection.hub.start()
    .done(function() { console.log("SignalR connection is up and running."); })
    .fail(function(error) { console.log(error); });
});

I don't know the exact reasoning that went into this, but the overloads, that are usually used to keep track of connected clients and possibly associating them with abstractions (something SignalR groups cannot provide since they are not enumerable) are fairly useless for the connections you cannot poke back, so that's my two cents on why it has to be done this way.

这篇关于SignalR分离事件多个集线器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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