How to send Parameter/Query in HubConnection SignalR Core

别说谁变了你拦得住时间么 提交于 2020-01-12 07:00:24

问题


I'm trying to add parameter into connection to signalr.

I'm using Builder to create my Client connection and start it:

var connection = new HubConnectionBuilder()
        .WithUrl("http://10.0.2.162:5002/connection")
        .WithConsoleLogger()
        .WithMessagePackProtocol()
        .WithTransport(TransportType.WebSockets)
        .Build();

await connection.StartAsync();

I want to send a simple parameter in this connection: Something Like:

"Token": "123"

In my server side i think i can take this parameter from HttpContext:

public override Task OnConnectedAsync()
{
    var httpContext = Context.Connection.GetHttpContext();
    var token = httpContext.Request.Query["Token"];
    return base.OnConnectedAsync();
}

Any idea of how to send this parameter? Thanks.


回答1:


I have found how to do this after much research:

On my build i just send the token from url connection. Like this:

var connection = new HubConnectionBuilder()
        .WithUrl($"http://10.0.2.162:5002/connection?token={token}")
        .WithConsoleLogger()
        .WithMessagePackProtocol()
        .WithTransport(TransportType.WebSockets)
        .Build();


来源:https://stackoverflow.com/questions/49038762/how-to-send-parameter-query-in-hubconnection-signalr-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!