Search code examples
asp.netsignalraspnetboilerplate

Can't call javascript client method in signalr


The following code works fine in IIS Express, but failed in IIS10.

The weird thing is serverside method can successfully be invoked, however clientside method can't.

JavaScript

var hub = $.connection.liveRoomHub;
hub.client.addMessageToPage = function(data){
    debugger;//here, this method never gets invoked
    console.log(JSON.stringify(data));
};
$.connection.hub.start()
    .done(function() {
        hub.server.join('room1')
        .done(function(){
            debugger; //code can run into here
            hub.server.sendMessage('user','test','room1');
        })
    });

C#

public class LiveRoomHub : Microsoft.AspNet.SignalR.Hub
{
    public ILogger Logger { get; set; }

    public async Task SendMessage(string name, string message, string roomName)
    {
        await Clients.Group(roomName)
            .addMessageToPage(new
            {
                Name = name,
                Message = message
            });
        Logger.Info($"{name}send msg:{message}in room:{roomName},");//logged
    }

    public async Task Join(string roomName)
    {
        await Groups.Add(Context.ConnectionId, roomName);
        Logger.Info($"{Context.ConnectionId} enter room: {roomName}");//logged
    }
}

Solution

  • All right, problem solved.

    I'm using aspnetboilerplate, and abp.signalr.js automatically calls the hub connection before my JavaScript code is loaded.

    Obviously, at that time, my hub.client.addMessageToPage isn't registered yet.

    That's the common Connection started before subscriptions are added error.