SignalR Core not generating(mapping) Client methods
Asked Answered
W

2

11

The SignalR Core is generating Hub proxies script, but not adding the "client" methods. (No errors in server or client - only not working)

Generated JS from <script src="http://localhost/signalr/hubs">

proxies['messageHub'] = this.createHubProxy('messageHub'); 
proxies['messageHub'].client = { };
proxies['messageHub'].server = {
        handleMessage: function (receivedString) {
            return proxies['messageHub'].invoke.apply(proxies['messageHub'], $.merge(["HandleMessage"], $.makeArray(arguments)));
         }
    };

Here's the Hub in Server Side:

public class MessageHub : Hub
{
    public void HandleMessage(string receivedString)
    {
        var responseString = string.Empty;

        MessageHandler.HandleMessage(receivedString, ref responseString);

        Clients.All.sendMessage(responseString);

    }
}

The sendMessage methos should be included in the messageHub client proxies in the JS file. $.connection.messageHub.client.sendMessage is undefined

Only the handleMessage for server proxies was created (and working).

Here's my StartUp.cs inclusions for SignalR:

ConfigureServices:

services.AddMvc(options =>
        {
            options.Filters.Add(new RoleFilterAttribute());
        }).AddJsonOptions(options => options.SerializerSettings.ContractResolver =
                new DefaultContractResolver());
services.AddSignalR(options => options.Hubs.EnableDetailedErrors = true)

Configure:

app.UseWebSockets();
app.UseSignalR();

project.json:

"Microsoft.AspNetCore.Mvc": "1.0.0-*",
"Microsoft.AspNetCore.WebSockets": "1.0.0",
"Microsoft.AspNetCore.SignalR.Server": "0.2.0-*",

SOME ADDITIONAL TENTATIVES:

1 - Change method case in Server Side to see if it's mapped:

Clients.All.SendMessage(responseString);

Did not work!

2 - Change the client side to dynamic mapping:

var connection = $.hubConnection('http://localhost/');
var proxy = connection.createHubProxy('messageHub');
connection.start({ withCredentials: false }).done(function () { console.log("CONNECTED") });
proxy.on("sendMessage", function (result) {console.log(result);});
proxy.invoke("handleMessage", msg).done(function(result)console.log(result);});

Again only the handleMessage (server) worked.

Wasserman answered 6/12, 2016 at 9:54 Comment(1)
I would suggest to follow the instructions provided in Tutorial: Get started with ASP.NET Core SignalRRosemari
E
0

Well according to the docs you are missing method name so the send all line should look like this

public void HandleMessage(string receivedString)
{
    var responseString = string.Empty;

    MessageHandler.HandleMessage(receivedString, ref responseString);

    Clients.All.SendMessage("SendMessage",responseString);

}

Also in the following is the correct way

app.UseSignalR(routes =>
        {
            routes.Hub<MessageHub>("/messageHub");
        });

and finally

    var connection = $.hubConnection('http://localhost/');
var proxy = connection.createHubProxy('messageHub');
connection.start({ withCredentials: false }).done(function () { console.log("CONNECTED") });
proxy.on("SendMessage", function (result) {console.log(result);});
proxy.invoke("HandleMessage", msg).done(function(result)console.log(result);});
Ellenaellender answered 1/12, 2018 at 22:35 Comment(0)
N
0

ASP.NET Core SignalR doesn't generate client proxies. There's good advice in the comment to follow the tutorial https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-6.0&tabs=visual-studio

Naivete answered 14/8, 2022 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.