gpt4 book ai didi

c# - 客户端未收到来自 Controller 的 SignalR 消息

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:55 24 4
gpt4 key购买 nike

我正在尝试使用 SignalR 从 Controller 向客户端组发送消息。当我的一个 Controller 中发生事件时,我需要使用信号器中心推送一条消息,以便在我的客户端屏幕上显示为警报。我知道这里问了很多问题,我已经阅读并尝试了很多。由于我是 SignalR 的新手,他们中的一些人甚至已经帮我把事情安排妥当了。目前一切似乎都已就绪。客户端可以连接到集线器并加入组, Controller 可以从集线器调用方法。但是客户从来没有收到消息,我也不知道为什么。我怀疑 Controller 调用的集​​线器方法没有“看到”客户端,但我不明白哪里出了问题。

集线器代码:

public static class UserHandler
{
public static HashSet<string> ConnectedIds = new HashSet<string>();
}

[HubName("myHub")]
public class MyHub : Hub
{
private static IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
public void Notify(string groupName, string message)
{
Clients.Group(groupName).displayNotification(message);
}

public static void Static_Notify(string groupName, string message)
{
var toto = UserHandler.ConnectedIds.Count();
hubContext.Clients.Group(groupName).displayNotification(message);
hubContext.Clients.All.displayNotification(message);//for testing purpose
}

public Task JoinGroup(string groupName)
{
return Groups.Add(Context.ConnectionId, groupName);
}

public Task LeaveGroup(string groupName)
{
return Groups.Remove(Context.ConnectionId, groupName);
}

public override Task OnConnected()
{
UserHandler.ConnectedIds.Add(Context.ConnectionId);
return base.OnConnected();
}

public override Task OnDisconnected(bool StopCalled)
{
UserHandler.ConnectedIds.Remove(Context.ConnectionId);
return base.OnDisconnected(StopCalled);
}
}

这是来 self 的 Controller 的调用:

//(For simplification and readability I define here variables actually obtained by treating some data )
//I already checked that problem did not come from missing data here
string groupName = "theGroupName";
string message = "My beautifull message.";

//prepare signalR call
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

//Following commented lines are different attempts made based on exemples and answers I found here and on others sites.
//The uncommented one is the one in use at the moment and is also based on an answer (from SO i think)

//context.Clients.Group(_userEmail).displayNotification(message);
//context.Clients.Group(_userEmail).Notify(_userEmail,message);
MyHub.Static_Notify(_userEmail, message);

这是客户端代码:

$(document).ready(function () {
var userGroup = 'theGroupName';
$.connection.hub.url = 'http://localhost/SignalRHost/signalr';
var theHub = $.connection.myHub;
console.log($.connection)
console.log($.connection.myHub)

theHub.client.displayNotification = function (message) {
console.log('display message');
alert(message);
};

$.connection.hub.start()
.done(function () {
theHub.server.joinGroup(userGroup);
console.log("myHub hub started : " + $.connection.hub.id)
console.log(theHub)
})
.fail(function () {
console.log('myHub hub failed to connect')
});

});

请帮助我理解我未能理解的逻辑或我遗漏的错误。

编辑:

回答 Alisson 的评论:我忘记显示的 Startup.cs

public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration { };
map.RunSignalR();
});
}

重要的事情我也忘了提:

  • Controller 、集线器和客户端是 3 个不同的项目(由于全局应用程序架构,我不得不将集线器逻辑分开)所有都在我的本地主机 IIS 上,但在不同的端口上
  • 我在“OnConnected”和“onDiconnected”事件上设置了断点,客户端连接和断开连接正常

最佳答案

您只需要一个应用程序充当服务器,在您的情况下,它应该是 SIgnalRHost 项目。您的 Controller 项目应该是服务器的客户端,因此它只需要这个包:

Install-Package Microsoft.AspNet.SignalR.Client 

您的 Controller 项目实际上不需要引用包含集线器类的项目。在您的 Controller 中,您将使用 C# SignalR 客户端连接到服务器(就像您在 javascript 客户端中所做的那样),加入组并调用集线器方法:

var hubConnection = new HubConnection("http://localhost/SignalRHost/signalr");
IHubProxy myHub = hubConnection.CreateHubProxy("MyHub");
await hubConnection.Start();
myHub.Invoke("JoinGroup", "theGroupName");
myHub.Invoke("Notify", "theGroupName", "My beautifull message.");

...最后,您根本不需要那个Static_Notify

Since you are passing the group name as a parameter on the Notify method, you don't really need to join the group from your controller. You'd need only if you were trying to send the message to the same group the controller was connected (then you wouldn't need to pass the group name as parameter anymore).

SignalR C# Client Reference .

关于c# - 客户端未收到来自 Controller 的 SignalR 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47890328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com