SignalR Groups not getting data

断了今生、忘了曾经 提交于 2019-12-22 20:41:09

问题


Hi I am having a problem where my signalR clients who are in a group do not get the information sent.

I have tried this in two ways and both not working.

On web page loaded the client will call the server to get the group name and then call a method on the hub to join the user in that group.

JavaScript.....

        var connectionOpen = false;
        var myHub;
        $(function () {
            myHub = $.connection.myHub;
            myHub.client.showMessage = alertMessage;
            $.connection.hub.start(function () {
                connectionOpen = true;
                joinGroup();
            });
        });

        function joinGroup() {
            $.ajax({
                url: 'Controller/GetGroupName,
                type: 'POST',
                success: function (group) {
                    if (connectionOpen == true)
                        tileHub.server.joinGroup(group);
                }
            });
        }

function alertMessage(string value){
alert(value);
}

Controller

[HttpPost]
public JsonResult GetGroupName()
{
    return Json("Foo");
}

myHub

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

I have stepped through the code numerous times and I can see that I am connecting ok and joining the group but when I try and send back to the group nothing happens

I am sending to the group using this from my controller

GlobalHost.ConnectionManager.GetHubContext<myHub>().Clients.Group("Foo").showMessage("Hello");

This is the second way I have tried, I have also tried sending to the groups from the hub but still no joy.

any help would be greatly appreciated...

Updated This has been fixed thanks to David Fowler....

This was a bug due to the fact that I was giving the Hub a different name using the HubName attribute and then signalR using the longPolling transport. Removing the attribute allows this to know work. A fix has also been provided please see https://github.com/SignalR/SignalR/issues/1039


回答1:


Few things:

  1. Make sure you read the "Rejoining Groups" section on this post. http://weblogs.asp.net/davidfowler/archive/2012/11/11/microsoft-asp-net-signalr.aspx.
  2. Do you really need to make an ajax call to figure out the group name from the server? Can't you just render it directly in the page?
  3. Does it fail if you hardcode "Foo" as the group name everywhere?



回答2:


Found in the anwser of dfowler: https://github.com/SignalR/SignalR/issues/1039 < Didn't see it in de comments.

The solution is: Remove the HubName attribute from your Hub.



来源:https://stackoverflow.com/questions/13531314/signalr-groups-not-getting-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!