【发布时间】:2017-07-30 07:23:58
【问题描述】:
我正在尝试使用 GetHubContext 从另一个项目连接到我的 SignalR 项目,但它无法正常工作。这是代码:
第一个项目
[HubName("DataHub")]
public class DataHub : Hub
{
int counter = 1;
public void Hello(String message)
{
Clients.All.addMessage(message);
}
public void getNewData(Dictionary<string,string> s)
{
Object result = new[] { s };
Clients.All.addnothing(result);
}
第二个项目:
class Class1
{
static void Main(string[] args)
{
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<Tape.DataHub>();
while(true){
Dictionary<string, string> data = new Dictionary<string, string> { };
data.Add("User", "PD");
data.Add("DeviceId","445");
data.Add("ID", "1");
Object result = new[] { data };
hubContext.Clients.All.addData(data);
System.Threading.Thread.Sleep(5000);
}
}
}
}
客户端的Javascript代码
$.connection.hub.url = "http://localhost:57844/signalr";
var dataHub = $.connection.DataHub;
dataHub.client.addData = function (message) {
console.log(message);
var obj = message;
for (var i = 0; i < obj.length; i++)
//loops through the data and adds new data to the html page
}
$.connection.hub.start().done(function () {
console.log("connected");
}).fail(function (error) {
console.log('Invocation of start failed. Error: ' + error)
});
客户端应通过信号器集线器从类 1 获取新数据,该信号器集线器与信号器项目位于不同的类上。但我没有得到任何新数据,客户端只连接到 DataHub。有什么遗漏吗?
【问题讨论】:
-
不,我不是自托管
-
但是做GetHubContext的项目好像是一个Console app?您只能从托管信号器服务的同一个 Appdomain 中执行 GetHubContext
-
是的,它是一个控制台应用程序,但在不同的项目上,而不是在托管信号器的同一个项目上。它可以在不同的项目上吗?我确实参考了它。我引用了项目和类: GlobalHost.ConnectionManager.GetHubContext
(); -
它们可以在不同的项目中,但它们需要托管在同一个应用程序域下(相同的进程)。如果不是,您可以使用服务总线在应用程序之间进行通信。喜欢hibernatingrhinos.com/oss/rhino-service-bus
标签: c# websocket signalr signalr-hub signalr.client