编程语言
首页 > 编程语言> > javascript – SignalR Client如何在开始连接时设置用户?

javascript – SignalR Client如何在开始连接时设置用户?

作者:互联网

服务器端:

public override Task OnConnected()
{
    var connectionId = Context.ConnectionId;
    var user = Context.User.Identity.Name; // Context.User is NULL
    return base.OnConnected();
}

客户端(在Console项目中):

IHubProxy _hub;
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.Start().Wait();

当客户端连接到服务器时,我想知道userName和connectionId之间的映射,但Context.User为NULL.如何在客户端设置此值?

解决方法:

使用查询字符串传递您的用户名

客户

首先设置查询字符串

string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.qs = { 'username' : 'anik' };
connection.Start().Wait();

服务器

public override Task OnConnected()
{
    var username= Context.QueryString['username'];
    return base.OnConnected();
}

标签:c,javascript,signalr,signalr-hub,signalr-client
来源: https://codeday.me/bug/20190702/1360770.html