【发布时间】:2018-12-05 04:21:30
【问题描述】:
我正在使用 React 前端的 ASP.NET Core 2.2 应用程序中实现 Azure SignalR 服务。当我发送消息时,我没有收到任何错误,但我的消息没有到达 Azure SignalR 服务。
具体来说,这是一个私人聊天应用程序,因此当消息到达中心时,我只需将其发送给特定聊天的参与者,而不是发送给所有连接。
当我发送消息时,它会到达我的集线器,但我看不到消息正在发送到 Azure 服务的迹象。
为了安全,我使用 Auth0 JWT Token 认证。在我的中心,我正确地看到了授权用户的声明,所以我认为安全性没有任何问题。正如我所提到的,我能够访问集线器这一事实告诉我,前端和安全性工作正常。
然而,在 Azure 门户中,我看不到任何消息的指示,但如果我正确读取数据,我确实看到 2 个客户端连接在我的测试中是正确的,即我用于测试的两个打开的浏览器。这是屏幕截图:
这是我的Startup.cs 代码:
public void ConfigureServices(IServiceCollection services)
{
// Omitted for brevity
services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions => {
jwtOptions.Authority = authority;
jwtOptions.Audience = audience;
jwtOptions.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
// Check to see if the message is coming into chat
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
(path.StartsWithSegments("/im")))
{
context.Token = accessToken;
}
return System.Threading.Tasks.Task.CompletedTask;
}
};
});
// Add SignalR
services.AddSignalR(hubOptions => {
hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(10);
}).AddAzureSignalR(Configuration["AzureSignalR:ConnectionString"]);
}
这是Configure() 方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Omitted for brevity
app.UseSignalRQueryStringAuth();
app.UseAzureSignalR(routes =>
{
routes.MapHub<Hubs.IngridMessaging>("/im");
});
}
这是我用来将用户的connectionId 映射到userName 的方法:
public override async Task OnConnectedAsync()
{
// Get connectionId
var connectionId = Context.ConnectionId;
// Get current userId
var userId = Utils.GetUserId(Context.User);
// Add connection
var connections = await _myServices.AddHubConnection(userId, connectionId);
await Groups.AddToGroupAsync(connectionId, "Online Users");
await base.OnConnectedAsync();
}
这是我的中心方法之一。请注意,我知道一个用户可能同时有多个连接。我只是简化了这里的代码以使其更易于消化。我的实际代码考虑了具有多个连接的用户:
[Authorize]
public async Task CreateConversation(Conversation conversation)
{
// Get sender
var user = Context.User;
var connectionId = Context.ConnectionId;
// Send message to all participants of this chat
foreach(var person in conversation.Participants)
{
var userConnectionId = Utils.GetUserConnectionId(user.Id);
await Clients.User(userConnectionId.ToString()).SendAsync("new_conversation", conversation.Message);
}
}
知道我做错了什么导致消息无法到达 Azure SignalR 服务吗?
【问题讨论】:
-
要进行故障排除,请尝试直接向已知连接发送消息,以查看消息是否可以通过。如果确实如此,那么您可以缩小聊天逻辑范围以找出可能的问题。
-
我确保消息到达集线器,调用正确的方法并将消息发送到正确的 ConnectionId。我仍然在门户网站上看到 0 条消息,并且在“活动日志”下什么也没有。除了 Azure 门户上的“概述”选项卡和“活动日志”下的消息指示器之外,还有其他方法可以查看消息是否到达 Azure SignalR 服务?
-
该问题是由 SignalR 的旧版本客户端脚本引起的。当我将它们升级到最新版本时,一切都开始正常了。
-
这部分在我看来是错误的。但可能不会导致您的问题。看起来它正在发送给请求呼叫的用户,而不是所有参与者 // 向此聊天的所有参与者发送消息 foreach(var person in conversation.Participants) { var userConnectionId = Utils.GetUserConnectionId(user.Id) ;等待 Clients.User(userConnectionId.ToString()).SendAsync("new_conversation", conversation.Message); }
-
在我使用 Azure 的 SignalR 的 3 个月中两次,该服务刚刚完全停止接收消息(尽管它仍在“运行”)。调用 SignalR 服务,发送消息,这是肯定的。但就接收而言,signalR 服务上没有任何记录。我的解决方案是发布一个新的 SignalR 服务并换掉连接细节。不是一个可行的长期解决方案。
标签: azure asp.net-core signalr asp.net-core-signalr azure-signalr