我已经部署了一组 Azure Functions (v2)。
我希望在服务器端发生某些事情时通知 iOS 应用程序,因此我创建了一个 Azure SignalR 服务,配置为Serverless
.
如文档中所述,我创建并部署了一个新的negotiate
Azure 函数,它在服务总线队列上发布订阅信息:
[FunctionName("negotiate")]
public async Task<SignalRConnectionInfo> Negotiate(
[HttpTrigger(AuthorizationLevel.Anonymous)]
HttpRequest httpRequest,
ClaimsPrincipal claimsPrincipal,
[SignalRConnectionInfo(HubName = "updates", UserId = "{headers.x-ms-client-principal-id}")]
SignalRConnectionInfo connectionInfo)
{
try
{
// Uses the name identifier for now.
Claim nameIdentifierClaim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
string userId = nameIdentifierClaim.Value;
foreach (Guid groupId in claimsPrincipal.GetGroups())
{
var subscription = new SynchronizationSubscriptionContract {UserId = userId, GroupId = groupId};
await m_serviceBus.SendAsync(JsonConvert.SerializeObject(subscription));
}
return connectionInfo;
}
catch (Exception exc)
{
//
}
}
服务总线绑定到将用户添加到组列表的 Azure 函数:
[ServiceBusAccount(Constants.Configuration.ServiceBusConnectionString)]
[FunctionName("subscribe")]
public async Task AddToGroupAsync(
[ServiceBusTrigger("synchronization-subscriptions")] string requestMessage,
[SignalR(HubName = "updates")] IAsyncCollector<SignalRGroupAction> signalRGroupActions)
{
var synchronizationSubscriptionContract = JsonConvert.DeserializeObject<SynchronizationSubscriptionContract>(requestMessage);
string groupName = synchronizationSubscriptionContract.GroupId;
var addGroupAction = new SignalRGroupAction
{
UserId = synchronizationSubscriptionContract.UserId,
GroupName = groupName,
Action = GroupAction.Add
};
await signalRGroupActions.AddAsync(addGroupAction);
}
到目前为止,一切都很好。
每当我现有的 Azure Functions 之一需要在 SignalR 通道上发布消息时,它都会在绑定到另一个 Azure Function 的专用服务总线队列上发送消息。然后 Azure 函数获取消息并将其发送到 Azure SignalR 服务:
[ServiceBusAccount(Constants.Configuration.ServiceBusConnectionString)]
[FunctionName(Api.Functions.Synchronization.UpdateSynchronizationInfo)]
public async Task Run(
[ServiceBusTrigger("synchronization-requests")] string requestMessage,
[SignalR(HubName = "updates")]
IAsyncCollector<SignalRMessage> signalRMessages
)
{
var requestContract = JsonConvert.DeserializeObject<SynchronizationUpdateRequestContract>(requestMessage);
var request = m_mapper.Map<SynchronizationUpdateRequest>(requestContract);
// Do more stuff.
string groupName = request.GroupId;
var updateMessage = new SignalRMessage
{
GroupName = groupName,
Target = "notify",
Arguments = new string[] {}
};
await signalRMessages.AddAsync(updateMessage);
}
根据我在 Azure Functions 中不同位置添加的日志,我没有看到任何错误,并且所有内容似乎都被正确调用。但是,我在 signalR 上没有看到任何消息:即使连接计数增加,消息计数也保持在 0。我的应用程序没有收到任何消息。
问题
为什么没有将消息发送到 Azure SignalR?我错过了什么?