2012-09-18 65 views
0

我有2个集线器类:hubA和hubB。无论如何发送消息从集线器类到另一个集线器的JavaScript处理函数

在虎霸我有一个执行任务的功能:

public void doSomething(string test){ 
    Clients[Context.ConnectionId].messageHandler(test); 
} 

我不想要这个功能后回hubA.messageHandler = function(){...}我希望能够发布一条消息回hubB.messageHandler = function(){...},但我打电话它来自我的hubA集线器类。这可能吗?

+0

你能解释一下好一点吗?也许再添加一些代码? – JotaBe

回答

1

如果两个毂在同一个应用程序托管,你就应该能够使用:

GlobalHost.ConnectionManager.GetHubContext<HubB>() 

现在的诀窍是,你似乎想将消息发送到特定的客户端上HUBB和问题HubA的Context.ConnectionId将不会是HubB的相同ID。因此,您需要做的是在HubA和HubB中将某种类型的ConnectionId映射到逻辑上的某种用户。然后,当您需要“弥合差距”时,您通过HubA的ConnectionId查找来自HubA的逻辑用户,然后查找HubB的ConnectionId。在这一点你的代码可能看起来像这样:

public void DoSomething(string test) 
{ 
    // Get HubB's ConnectionId given HubA's ConnectionId (implementation left to you) 
    string hubBConnectionId = MapHubAConnectionIdToHubBConnectionId(Context.ConnectionId); 

    // Get the context for HubB 
    var hubBContext = GlobalHost.ConnectionManager.GetHubContext<HubB>(); 

    // Invoke the method for just the current caller on HubB 
    hubBContext.Clients[hubBConnectionId].messageHandler(test); 
} 
相关问题