2

For the last few days I have been trying to implement SignalR into my AngularJS/WebAPI application.

I have been able to successfully send/receive messages from client to client, however when I push messages purely from the Server, none of the clients receive any messages.

I have seen many people having the same problem, the answer always seems to be using GlobalHost.ConnectionManager.GetHubContext which I have been implementing, without error, however the clients still don't receive any of the messages.

I thought that perhaps it's because WebAPI calls are asynchronous and and therefore takes place on a different thread, but I can't be sure. Please could someone have a look and tell me what I'm doing wrong.

This is my Hub Class:

 public class ChatHub : Hub
{

    public void Send(string name, string message)
    {
        // Call the broadcastMessage 
        Clients.All.broadcastMessage(name, message);
    }

    public void RunMe()
    {
        System.Diagnostics.Debug.WriteLine("Client Started");
    }

    public static void Notify(string name, string message)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        hubContext.Clients.All.broadcastMessage(name, message);
    }

}    

This is the Angular Controller in Javascript:

$scope.chat = $.connection.chatHub;

$scope.chat.client.broadcastMessage = function (name, message) {
    $scope.$apply(function () {
        var scope = angular.element($('#discussion')).scope();
        scope.chatMessage = message;

        alert(message);
    });
};

$.connection.hub.start()
.done(function ()
{
    console.log('Now connected, connection ID=' + $.connection.hub.id);
    $scope.chat.server.runMe();
})
.fail(function(){ console.log('Could not Connect!'); });

$('#sendmessage').click(function () {
    $scope.chat.server.send("SERVER", $('#inputMessage').val());
});    

This is the Controller that is trying to notify the clients from the server

    public class UserController : ApiController
{
    #region METHODS

    [ActionName("Create")]
    [HttpPost]
    public HttpResponseMessage Create(JObject parameters)
    {

        //DYNAMIC DATA
        dynamic data = parameters;

        //CHECK IF CALL FAILED
        if (data == null)
            return Request.CreateResponse(HttpStatusCode.InternalServerError, "Request is null");

        //PERFORM REQUEST 
        using (var svc = new UserService())
        {
            //SET Parameters
            String Username = data.Username;
            String Password = data.Password;

            //NOTIFY USERS
            ChatHub.Notify("SERVER", "SERVER MESSAGE");

            //CREATE Response
            var response = svc.Create(Username, Password);

            //RESPOND
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }
    }

}

So just to reiterate, when the "sendmessage" button is clicked on my UI, it sends a message to the server which is then received again by the clients, this works 100%,

However when I call the static Notify method from the Controller None of the clients receive any messages.

Calling the function does not return any errors.

Please could someone help me!

Stephan
  • 89
  • 2
  • 5
  • why are you using jquery in the controllers? create a signalr service wrapper – pedrommuller Feb 03 '15 at 13:26
  • @jack.the.ripper Thanks for the reply. Do you have a small example using the signalr service wrapper? I am new to this and just cannot figure it out. Thanks in advance. – Stephan Feb 04 '15 at 13:29
  • Pretty sure he is talking about angular services; does this help: http://stackoverflow.com/questions/20139127/signalr-sending-data-using-globalhost-connectionmanager-not-working ? – Lars Höppner Feb 14 '15 at 03:36
  • http://stackoverflow.com/questions/20561196/signalr-calling-client-method-from-outside-hub-using-globalhost-connectionmanage – Lars Höppner Feb 14 '15 at 03:51
  • hmm.. actually I tried this just now, and it works for me – Lars Höppner Feb 14 '15 at 04:07
  • could you try calling Notify on a separate thread? also: are you sure the client is already connected to the hub when you make that call to Notify? can you add the SignalR version + startup configuration? – Lars Höppner Feb 14 '15 at 04:13
  • and .. last comment: are you using IIS express as web server in VS? – Lars Höppner Feb 14 '15 at 04:19

2 Answers2

0

    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-2.2.0.min.js"></script>

Check your jQuery.signalR version.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
0

If you are using dependency injection, the example at ASP.NET is wrong, you have to set your GlobalHost.DependendcyResolver in the the Global.asax file. not in the startup class.

Kelso Sharp
  • 972
  • 8
  • 12