客户端未收到来自控制器的 SignalR 消息

Client not receiving SignalR message from controller

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

集线器代码:

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);
    }
}

这是来自我的控制器的调用:

//(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();
        });
    }

重要的事情我也忘了说:

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

Install-Package Microsoft.AspNet.SignalR.Client 

您的控制器项目实际上不需要引用包含集线器的项目 class。在您的控制器中,您将使用 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.