angularjs 特定的 SignalR 客户端
SignalR client specific with angularjs
所以我对 signalR 还很陌生,并且在 MVC 中使用过它。现在我在 angularjs 的 webapi 中使用它并且有点困惑或者忘记了我做了什么。我在 webapi 中使用不记名令牌并尝试创建一个通知系统。
我想弄清楚的是将 angularjs 与 signalR 一起使用的正确方法。我看到很多人使用代理 on/invoke。 proxy.on 是当我从服务器调用 hubcontext 时:
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
hubContext.Clients.User(UserId).broadcastNotification("Good morning! The time is " + DateTime.Now.ToString());
和proxy.invoke方法来自客户端?如果是这样,那将是使用通知系统的最佳方式(我假设 proxy.on)?
我的第二个问题更多是关于向特定用户发送通知。对于向特定用户发送请求,我假设您希望在集线器上这样做:
public void SendNotification(string userId)
{
Clients.User(userId).broadcastNotification("Good morning! The time is " + DateTime.Now.ToString());
}
我的启动是这样的:
public class MyProvider : IUserIdProvider
{
public string GetUserId(IRequest request)
{
var userId = request.User.Identity.Name;
return userId.ToString();
}
}
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AuthContext, Travelfy.API.Migrations.Configuration>());
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new MyProvider());
app.MapSignalR("/hubs", new HubConfiguration());
}
当我刷新页面时,我注意到我所有的用户标识都是空字符串“”。我读到这可能是由于使用了不记名令牌。如果是这样,我将如何使用不记名令牌来指定我要发送到的 userId?当我使用 Clients.All 时一切正常,所以我假设它必须与我得到的 startup/userIds 相关。
谢谢
回答你的第一个问题:
Which would be the best way for using notification systems
如果你想从服务器向客户端推送通知,你必须定义一个新的处理程序是在客户端(使用生成的代理)定义一个方法,如下所示:
How to define methods on the client that the server can call
如果你想让客户端调用一个位于服务器上的方法,你必须使用这个方法:
How to call server methods from the client
回答你的第二个问题:
For sending requests to specific users, I would assume you would want
to do this on the hub
您可以使用您希望定位的客户的 connection ID
。看到这个:
所以过了一会儿,我想出了正确的答案。因为我使用的是 bearerTokens,所以我真的不得不确定另一种获取 userId 的方法,而不是仅仅依赖 request.User.Identity.Name。我需要做的是将我的 bearerToken 传递给 connection.qs 值。
connection.qs = { Bearer: token };
一旦我能够做到这一点,我就必须根据我发送的令牌来查找我的用户。
var token = request.QueryString.Get("Bearer");
var authenticationTicket = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(token);
所以我对 signalR 还很陌生,并且在 MVC 中使用过它。现在我在 angularjs 的 webapi 中使用它并且有点困惑或者忘记了我做了什么。我在 webapi 中使用不记名令牌并尝试创建一个通知系统。
我想弄清楚的是将 angularjs 与 signalR 一起使用的正确方法。我看到很多人使用代理 on/invoke。 proxy.on 是当我从服务器调用 hubcontext 时:
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
hubContext.Clients.User(UserId).broadcastNotification("Good morning! The time is " + DateTime.Now.ToString());
和proxy.invoke方法来自客户端?如果是这样,那将是使用通知系统的最佳方式(我假设 proxy.on)?
我的第二个问题更多是关于向特定用户发送通知。对于向特定用户发送请求,我假设您希望在集线器上这样做:
public void SendNotification(string userId)
{
Clients.User(userId).broadcastNotification("Good morning! The time is " + DateTime.Now.ToString());
}
我的启动是这样的:
public class MyProvider : IUserIdProvider
{
public string GetUserId(IRequest request)
{
var userId = request.User.Identity.Name;
return userId.ToString();
}
}
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AuthContext, Travelfy.API.Migrations.Configuration>());
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new MyProvider());
app.MapSignalR("/hubs", new HubConfiguration());
}
当我刷新页面时,我注意到我所有的用户标识都是空字符串“”。我读到这可能是由于使用了不记名令牌。如果是这样,我将如何使用不记名令牌来指定我要发送到的 userId?当我使用 Clients.All 时一切正常,所以我假设它必须与我得到的 startup/userIds 相关。
谢谢
回答你的第一个问题:
Which would be the best way for using notification systems
如果你想从服务器向客户端推送通知,你必须定义一个新的处理程序是在客户端(使用生成的代理)定义一个方法,如下所示:
How to define methods on the client that the server can call
如果你想让客户端调用一个位于服务器上的方法,你必须使用这个方法:
How to call server methods from the client
回答你的第二个问题:
For sending requests to specific users, I would assume you would want to do this on the hub
您可以使用您希望定位的客户的 connection ID
。看到这个:
所以过了一会儿,我想出了正确的答案。因为我使用的是 bearerTokens,所以我真的不得不确定另一种获取 userId 的方法,而不是仅仅依赖 request.User.Identity.Name。我需要做的是将我的 bearerToken 传递给 connection.qs 值。
connection.qs = { Bearer: token };
一旦我能够做到这一点,我就必须根据我发送的令牌来查找我的用户。
var token = request.QueryString.Get("Bearer");
var authenticationTicket = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(token);