1

This is my HTML:

<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var chat = $.connection.khaosHub;

        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (message) {
            // Html encode display name and message.
            var encodedMsg = $('<div />').text(message).html(); 
            // Add the message to the page.
            $('#discussion').append('<li>' + encodedMsg + '</li>');
        };

        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                console.log("sending");
                // Call the Send method on the hub.
                chat.server.send("something");
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
</script>

My Hub:

public class KhaosHub : Hub
{
    public void Send(string message)
    {
        Clients.All.broadcastMessage(message);
    }
}

When I click #sendmessage my Send method in KhaosHub is triggered which I have verified using a breakpoint and my message does get sent to the div via broadcastMessage.

Note: I've not included my call to app.MapSignalR in the example above as I know it's working from the client side.

The issue I have is when I call broadcastMessage from some back end code it doesn't work. I am calling it via:

var context = GlobalHost.ConnectionManager.GetHubContext<KhaosHub>();
context.Clients.All.broadcastMessage("some message");

When I debug the Clients.All property, I can't see any clients (I don't know if I should be able to but thought I'd add that information.

Any ideas?

EDIT: This is my startup class for the hub:

[assembly: OwinStartup(typeof (Startup))]

namespace CC.Web
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}
webnoob
  • 15,747
  • 13
  • 83
  • 165
  • 1
    are you using a custom dependency resolver? if so: http://stackoverflow.com/questions/20139127/signalr-sending-data-using-globalhost-connectionmanager-not-working – Lars Höppner Jul 18 '14 at 18:00
  • That could well be it! I'll review it when I get back into the code later – webnoob Jul 18 '14 at 18:02
  • @LarsHöppner - I'm using Autofac for DI btu can't see how the linked post could help in my situation. That appears to be related to injecting dependencies into my hub but Im just trying to call my hub. Have I missed something? – webnoob Jul 18 '14 at 21:01
  • @LarsHöppner - I've added my Startup class which I believe is what you were requesting. – webnoob Jul 18 '14 at 22:20
  • So does `Clients.All.broadcastMessage(message)` work from inside of the hub? Is the issue here that it's not successfully getting the hub context from outside of the Hub class. I've just looked at my working code from one of my projects which is very similar, and your code is correct. Are you referencing SignalR in your using directives in your class? – adaam Jul 18 '14 at 22:45
  • When you say from inside the hub, do you mean from my HTML page? If so, then yes it's working from there. It's just from the code behind I'm having issues. Apologies if I am misunderstanding the terminolgies with this. – webnoob Jul 18 '14 at 22:51
  • If you take a look at my code page here, I'm doing something similar to you (referencing hub from external code).. https://github.com/adaam2/FinalUniProject/blob/master/TwitterLogic/TwitterStream.cs - if you could edit your question to feature the full class where you are trying to call the hub from (i.e. the "back end" code you speak of in your question) – adaam Jul 18 '14 at 22:56
  • Ok thanks, I'll try to get something together to better explain the issue. If it helps I am calling the hub from some code that is in a separate assembly to the one I am calling app.MapSignalR. It's a pluggable solution so not so simple to demonstrate. I'll look at the links, thanks. – webnoob Jul 18 '14 at 23:05
  • @webnoob I'd think the separate assembly thing is most probably the root cause of the problem, see: http://stackoverflow.com/questions/15909430/signalr-hub-in-separate-dll - I'd pursue this line of enquiry the most. – adaam Jul 18 '14 at 23:17

1 Answers1

1

Thanks for the question. Following up on the comments I have tracked my own problem down also to not getting the correct hubcontext from the GlobalHost.ConnectionManager.

To solve this I specifically set a DependencyResolver on the GlobalHost and passing this Resolver to the HubConfiguration used to MapSignalR.

In code that is:

Microsoft.AspNet.SignalR.GlobalHost.DependencyResolver = 
    New Microsoft.AspNet.SignalR.DefaultDependencyResolver

app.MapSignalR(
    New Microsoft.AspNet.SignalR.HubConfiguration With
    {.Resolver = Microsoft.AspNet.SignalR.GlobalHost.DependencyResolver})

You may want to convert this VB.Net code to C#.

Jorrit Reedijk
  • 598
  • 3
  • 11
  • 22