Hub是SignalR的关键,我们先看一下Hub的定义:
namespace Microsoft.AspNetCore.SignalR
{
//
// 摘要:
// A base class for a SignalR hub.
public abstract class Hub : IDisposable
{
protected Hub();
//
// 摘要:
// Gets or sets an object that can be used to invoke methods on the clients connected
// to this hub.
public IHubCallerClients Clients { get; set; }
//
// 摘要:
// Gets or sets the hub caller context.
public HubCallerContext Context { get; set; }
//
// 摘要:
// Gets or sets the group manager.
public IGroupManager Groups { get; set; }
public void Dispose();
//
// 摘要:
// Called when a new connection is established with the hub.
//
// 返回结果:
// A System.Threading.Tasks.Task that represents the asynchronous connect.
public virtual Task OnConnectedAsync();
//
// 摘要:
// Called when a connection with the hub is terminated.
//
// 返回结果:
// A System.Threading.Tasks.Task that represents the asynchronous disconnect.
public virtual Task OnDisconnectedAsync(Exception? exception);
//
// 摘要:
// Releases all resources currently used by this Microsoft.AspNetCore.SignalR.Hub
// instance.
//
// 参数:
// disposing:
// true if this method is being invoked by the Microsoft.AspNetCore.SignalR.Hub.Dispose
// method, otherwise false.
protected virtual void Dispose(bool disposing);
}
}
第一是Clients,通过这个属性可以调用客户端的JS代码,实现通讯。
- Clients.All:所有连接的客户端
- Clients.Others:除了发送者之外的客户端
- Clients.Caller:调用方法的当前客户端
- Clients.Group(groupName):某一组客户端
然后是Context,包括当前连接的上下文信息,包括下面的属性: - Items:用于数据传递的键值对
- ConnectionId:当前连接的标识
- User:表明当前用户的ClaimsPrincipal
接下来是Groups,管理组,包括将连接添加到组和从组中移除。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...