SignalR在HubCallerContext中缺少连接

时间:2018-08-19 23:05:50

标签: signalr asp.net-core-2.0

尝试进行简单聊天并将用户连接到userTracker时将其发送到

 public override async Task OnConnectedAsync()
    {
        var user = Helper.GetUserInformationFromContext(Context);
        await this.userTracker.AddUserAsync(Context.Connection, user);
        await Clients.All.SendAsync("UsersJoined", new UserInformation[] { user });
        await Clients.All.SendAsync("SetUsersOnline", await GetOnlineUsersAsync());

        await base.OnConnectedAsync();
    }

但在旧版本中,HubCallerContext如下所示:

公共HubCallerContext(HubConnectionContext连接);

    public HubConnectionContext Connection { get; }
    public ClaimsPrincipal User { get; }
    public string ConnectionId { get; }

我正在使用的版本(2.3.0)就像

 protected HubCallerContext();


    public abstract string ConnectionId { get; }

    public abstract string UserIdentifier { get; }

    public abstract ClaimsPrincipal User { get; }

    public abstract IFeatureCollection Features { get; }

    public abstract CancellationToken ConnectionAborted { get; }



    public abstract void Abort();

那么我如何获得缺少的连接?

1 个答案:

答案 0 :(得分:0)

您只需将其注入使用位置

示例:

public class YourClassWhereYouNeedHubContext 
{
// Inject into constructor
public YourClassWhereYouNeedHubContext (IHubContext<VarDesignHub> hubcontext)
{
    HubContext = hubcontext;
    ...
}

private IHubContext<VarDesignHub> HubContext
{
    get;
    set;
}
}

那你也可以打电话

await this.HubContext.Clients.All.InvokeAsync("Completed", id);

也请阅读:

Call SignalR Core Hub method from Controller