문제

I realize there are breaking changes between SignalR and ASP.NET SignalR and I changed my code to accommodate that. But for some reason, I'm not able to figure out the right change.

The issue I'm having is that the server is not sending messages to the client.

Old SignalR code:

Server

var context = GlobalHost.ConnectionManager.GetHubContext<Dashboard>();
var subscribers = context.Clients;

if (!string.IsNullOrWhiteSpace(message.Source))
{
    var subscriber = subscribers[message.Source];

    subscriber.messageReceived(message);
}

Client

$.connection.dashboard.messageReceived = function(){...};

New code (ASP.NET SignalR):

Server

var context = GlobalHost.ConnectionManager.GetHubContext<Dashboard>();
var subscribers = context.Clients;

if (!string.IsNullOrWhiteSpace(message.Source))
{
    var subscriber = subscribers.Group(message.Source);             
    subscriber.messageReceived(message);
}

Client

$.connection.dashboard.client.messageReceived = function(){...};

Can someone help me figure out what's going wrong here?

도움이 되었습니까?

해결책

I was able to get your code logic to work. Here's what I built off your logic:

SERVER:

public class Status : Hub
{
    public override Task OnConnected()
    {
        Groups.Add(Context.ConnectionId, "foo");
        return base.OnConnected();
    }

    public void foo()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<Status>();
        var subscribers = context.Clients;
        var subscriber = subscribers.Group("foo");
        subscriber.messageReceived("ello");
    }
}

CLIENT:

var status = $.connection.status;

status.client.messageReceived = function (val) {
    alert(val);
}

$.connection.hub.start().done(function() {
    status.server.foo();
});

My only thoughts onto why your implementation is not working is:

  1. You are not adding the connection ID to the group that you're trying to broadcast to (see my OnConnected function on the server).
  2. In your .client method for messageReceived you are not allowing for a value to be passed as a paramter. Unless you're parsing the arguments object within the function you will not be able to get the message value.
  3. Your message.Source is incorrect

Hope this helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top