2016-03-02 126 views
1

我想建立一个signalR系统。signalr - 与不同的客户端

我有示例代码工作,使用两个浏览器和相同的集线器。消息被发送和接收。

现在,当我创建了一个不同的页面,并尝试将消息发送到集线器时,它似乎有点工作,这意味着它不会炸毁,但没有任何内容会传输到其他客户端。

我以为我是从所有的客户端访问同一个消息中心,但也许我错过了一些东西。

是否有可能将不同的网站连接到同一个消息中心?

开始编辑

按照要求....这里是我用我的第二个客户端的代码...

var connection = $.hubConnection('http://xxxxxxxxx.azurewebsites.net/'); 
    var contosoChatHubProxy = connection.createHubProxy('MessagePump'); 
// contosoChatHubProxy.on('Send', function (name, message) {console.log(name + ' ' + message);}); 


    $.connection.hub.start() 
.done(function() { 
    console.log('Now connected, connection ID=' + $.connection.hub.id); // returns an ID 
    //  $.connection.hub.send('testing', 'this is a test from the client'); 
    //  contosoChatHubProxy.send("testing"); 
    //  contosoChatHubProxy.invoke('testing', 'this is a test for the client 1'); 
    //  contosoChatHubProxy.invoke('say', 'this is a test for the client 2'); 
    //  contosoChatHubProxy.invoke('Send', 'This is a test for client 3'); 
    //  $.connection.hub.send('testing', 'this is a test from the client 4'); 
    contosoChatHubProxy.invoke('messagePump', 'user', 'this is a test message for 5'); 
}) 
.fail(function(){ console.log('Could not Connect!'); }); 

这是我的萤火我看到

enter image description here

从我能做的代码,代理似乎是本地加载,甚至没有看到远程系统集线器...

仅连接到远程系统集线器的我的控制台应用程序能够发送和接收消息。

顺便说一句 - 我已经试过上可以小写(MessagePump,messagePump) 但它并没有改变结果。

+0

你应该为了得到援助 – drneel

+0

没错添加相关代码,你需要证明你的工作,不然就很难说了什么问题是:-)我猜你会需要重写Hub实例的OnConnected方法,以便将连接的客户端放入适当的组中,然后确保将消息发送到正确的组。你的用例很正常。 – keithl8041

回答

0
var connection = $.hubConnection('http://xxxxxxxxx.azurewebsites.net/'); 

您正在尝试连接其他网站。这http://xxxxxxxxx.azurewebsites.net/应该让跨域请求。否则你不能连接。如果你能够http://xxxxxxxxx.azurewebsites.net/,你应该配置signalr像:

public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      // Branch the pipeline here for requests that start with "/signalr" 
      app.Map("/signalr", map => 
      { 
       // Setup the CORS middleware to run before SignalR. 
       // By default this will allow all origins. You can 
       // configure the set of origins and/or http verbs by 
       // providing a cors options with a different policy. 
       map.UseCors(CorsOptions.AllowAll); 
       var hubConfiguration = new HubConfiguration 
       { 
        // You can enable JSONP by uncommenting line below. 
        // JSONP requests are insecure but some older browsers (and some 
        // versions of IE) require JSONP to work cross domain 
        // EnableJSONP = true 
       }; 
       // Run the SignalR pipeline. We're not using MapSignalR 
       // since this branch already runs under the "/signalr" 
       // path. 
       map.RunSignalR(hubConfiguration); 
      }); 
     } 
    }