Skip to content

SignalR client base on console cannot receive message from server #44617

@CwjXFH

Description

@CwjXFH

I write a SignalR server to push stock data to fronts and I write a SignalR client base on console, it cannot receive message from server, client versions:

.NET6
Microsoft.AspNetCore.SignalR.Client 6.0.10
Microsoft.Extensions.Hosting 6.0.1

Client code:

var connection = new HubConnectionBuilder().WithUrl("http://localhost:5247/stockTrade").Build();
connection.On("Receive", (StockTradeInfo message) =>
{
    // ...
});
connection.Closed += ex =>
{
    if (ex != null)
    {
        Console.WriteLine($"connection closed with an exception:{ex}");
    }
    else
    {
        Console.WriteLine("connection closed");
    }

    return Task.CompletedTask;
};

await connection.StartAsync();
await connection.InvokeAsync("StockTradeInfo", stockId);

IHost host = Host.CreateDefaultBuilder().Build();
host.Run();

But, I can receive message use JS client:

let conn = new signalR.HubConnectionBuilder()
    .withUrl("http://localhost:5247/stockTrade")
    .build();

conn.start().then(() => {
    console.log("connection start");
    conn
        .invoke("StockTradeInfo", stockId)
        .then(() => console.log("send request"))
        .catch(reason => console.log(reason));
});

conn.onclose(error => {
    if (error !== undefined) {
        console.error(error);
    } else {
        console.info("closed");
    }
})

conn.on("receive", function (message) {
    if (!message) {
        return;
    }

    for (let data of message) {
        let tr = document.getElementById(`tr-${data.stockCode}`);
        if (tr === null) {
            addTrElement(data);
        } else {
            updateTdElementValue(tr, data);
        }
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-signalrIncludes: SignalR clients and servers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions