Pergunta

I'm using SignalR RC2, this is my hub

public class ImgHub : Hub
{
    public void Create(string guid)
    {
        Groups.Add(Context.ConnectionId, "foo");
    }

    public void SendMsg(string msg)
    {
        Clients.Group("foo").send(msg);
    }
}

I have a console application and a webapplication (asp.net webforms) that connect to this hub. the console application works just as I would expect, the problem is in the Javascript part. The "send" callback doesn't fire when I'm using Clients.Group in SendMsg, if I change SendMsg to this

public void ShareImage(byte[] image, string guid)
{
    Clients.All.ReceiveImage(image);
}

it works. Here is the Javascript code

<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/jquery.signalR-1.0.0-rc2.min.js"></script>
<script src="http://localhost:4341/signalr/hubs/" type="text/javascript"></script>
<script type="text/javascript">
    var mainHub;
    $(function () {
        $.connection.hub.url = 'http://localhost:4341/signalr';
        // Proxy created on the fly
        mainHub = $.connection.imgHub;

        mainHub.client.send = function (msg) {
            alert(msg);
        };

        // Start the connection
        $.connection.hub.start(function() {
            mainHub.server.create('vanuit den JS');
        })
            .done(function() {
                $('#msgButton').click(function() {
                    mainHub.server.sendMsg("msg from JS");
                });
            });
    });

</script>

as you can see in the JS code, I also have a button on the page that calls the SendMsg function, the message does arrive on the console application so I would guess that the JS client is correctly registered in the SignalR group.

I'm no JS specialist so I hope someone that knows more about it then I do can help me out here.

Foi útil?

Solução

It's because you need to enable rejoining groups in global asax.

GlobalHost.HubPipeline.EnableAutoRejoiningGroups();

There's more detail about that here: http://weblogs.asp.net/davidfowler/archive/2012/11/11/microsoft-asp-net-signalr.aspx

This method call is going away for 1.0 RTM but for now you need to do it.

Outras dicas

One of the reasons why your send function may not be executing is because by the time you are allowing a call to sendMsg on the client the client may not be in the group "foo" yet.

In your $.connection.hub.start you're registering a function to be called when start has completed, but you're also then registering another function to be called once start has completed via the .done. Therefore, what's happening is both functions are firing almost simultaneously. So when the sendMsg function is available to be called you may not have been successfully added to the group.

Here's how you can fix that problem:

$.connection.hub.start().done(function() {
    mainHub.server.create('vanuit den JS').done(function() {
        $('#msgButton').click(function() {
            mainHub.server.sendMsg("msg from JS");
        });
    });
});

Essentially I'm waiting until the group join has completed successfully until allowing a sendMsg to go through.

I know that this is a long shot answer since you're probably waiting a significant amount of time after the connection has been started and still nothings coming over the wire but I'm unable to replicate the behavior on my end.

If my fix above does not work you should ensure that your server side functions are being called by setting break points.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top