Javascript 要从.net客户端调用signalr集线器服务器,集线器服务器将调用web客户端进行更新
我的.net客户端代码是:Javascript 要从.net客户端调用signalr集线器服务器,集线器服务器将调用web客户端进行更新,javascript,.net,signalr,signalr-hub,signalr.client,Javascript,.net,Signalr,Signalr Hub,Signalr.client,我的.net客户端代码是: var hubConnection = new HubConnection("http://localhost/signalrServer"); IHubProxy myproxy = hubConnection.CreateHubProxy("ChatHub"); hubConnection.Start().Wait(); //hubConnection.ma mypro
var hubConnection = new HubConnection("http://localhost/signalrServer");
IHubProxy myproxy = hubConnection.CreateHubProxy("ChatHub");
hubConnection.Start().Wait();
//hubConnection.ma
myproxy.Invoke("JoinGroup", "MapRoom");
while (true)
{
System.Threading.Thread.Sleep(1000);
myproxy.Invoke("callFromService", new Random().NextDouble().ToString());
}
我的信号中心服务器代码:
public class ChatHub : Hub
{
public void CallFromService(string message)
{
// Call the broadcastMessage method to update clients.
//Clients.All.broadcastMessage(name, message);
Clients.All.broadcastMessage(message);
}
}
我的web客户端代码是
$(function () {
debugger;
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
//chat.logging = true;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (message) {
debugger;
angular.element(document.getElementById('mapdiv')).scope().$apply(function (scope) {
scope.mapMsg = message;
});
};
// Get the user name and store it to prepend to messages.
$.connection.hub.start().done(function () {
debugger;
//chat.server.joinGroup("MapRoom");
});
});
此代码只能在一个web客户端上工作
如果我在新选项卡或新浏览器上打开同一页,它将被卡住。
当我关闭一个标签,然后它的工作
我希望服务器更新所有web客户端。我尝试过:GlobalHost.ConnectionManager.GetHubContext().clients.all.broadcastMessage(message);(它不工作)你在哪里托管你的网站?如果它位于某种负载平衡器的后面,则有可能您没有连接到两个选项卡上的同一节点,并且除非您有背板,否则消息不会在节点之间同步。