0

Trying to do simple chat and sending user to the userTracker when he/she is connected

 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();
    }

but in the old versions HubCallerContext is like this :

public HubCallerContext(HubConnectionContext connection);

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

the version I am using ( 2.3.0 ) is like

 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();

So how can I get the missing Connection ?

1 Answers1

0

You simple have to inject it where you use it

Sample:

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

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

Then you can also call

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

Please read also:

Call SignalR Core Hub method from Controller

Stephu
  • 3,236
  • 4
  • 21
  • 33