0
我有這個樞紐:如何從控制器獲取連接ID?
public class NotificationHub : Hub<INotificationHub>
{
private readonly static ConnectionMapping<User> connections = new ConnectionMapping<User>();
public override Task OnConnected()
{
var user = GetUser();
connections.Add(user, Context.ConnectionId);
Groups.Add(Context.ConnectionId, user.GroupId.ToString());
return base.OnConnected();
}
public string GetConnectionId(User user)
{
return connections.GetConnections(user).First();
}
private User GetUser() {}
}
並在控制器我做:
var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
context.Clients.Groups(user.GroupId.ToString()).Notify();
但上面發送給該組的所有用戶。我如何才能將它發送給當前發出請求的用戶?
注: 我從集線器外部調用該方法。
現在我明白你在問什麼了,但是恐怕在[這裏]之前詢問過了(https://stackoverflow.com/questions/17534505/call-hub-method-in-c-sharp-code ) – meJustAndrew
[C#代碼中的Call Hub方法]的可能重複(https://stackoverflow.com/questions/17534505/call-hub-method-in-c-sharp-code) – meJustAndrew