如何将信号器应用于不同的网址?

How to apply signalr to different urls?

看下面这个jQuery格式,我是用signalr在某个view里面直播内容的

到目前为止,我只能在一个特定的视图中实时播放内容,即'/MyController/MyActionMethod'

现在我有两个视图可以播放与第一个视图相同的内容,它们是

第一个视图中引用了下面的 jQuery 代码,现在我添加了一些语法,例如在其他两个视图中启用相同的代码:

$.connection.hub.url = 'http://localhost:1334/MyController2/MyActionMethod2';  
$.connection.hub.url = 'http://localhost:1334/MyController3/MyActionMethod3';

但是这段代码没有做任何事情。

我在这里错过了什么?有什么我需要补充的吗?我还需要在两个视图中引用该代码吗?请指教,谢谢

$(function() { 
    // Save the reference to the SignalR hub 
    var comHub = $.connection.commentHub;
    comHub.client.newContent = function () {
        // Sample functions to refresh the page based on information coming 
        // from the server.
        RefreshPage();
        UpdateContent();

    };

    // Invoke the function to be called back from the server 
    // when changes are detected  

    $.connection.hub.url = 'http://localhost:1334/MyController2/MyActionMethod2';
    $.connection.hub.url = 'http://localhost:1334/MyController3/MyActionMethod3';

    // Start the SignalR client-side listener
    $.connection.hub.start().done(function () { 
  $("#pushbutton").click(function () {
    //...some function to trigger here
})

})

})

我想我明白你在问什么,你不需要添加这两行代码:

$.connection.hub.url = 'http://localhost:1334/MyController2/MyActionMethod2';
$.connection.hub.url = 'http://localhost:1334/MyController3/MyActionMethod3';

$.connection.hub.url 用于覆盖客户端用于连接到您的集线器的路由 URL,如 here 所述(无论如何,第二行覆盖第一行)。

您需要做的是在所有需要接收数据的三个视图中添加连接到 SignalR Hub 的代码,并管理这些数据的处理方式。