azure-signalr - 无法连接到 Azure SignalR ServerlessHub 实例
问题描述
我无法连接到 ServerlessHub 实例。
我正在使用这个参考文档。
客户:
var hubClient = new ClientSignalR();
await hubClient.Initialize("http://localhost:7093/api/LocationHub");
...
public async Task Initialize(string connectionUrl)
{
_connectionUrl = connectionUrl;
_hub = new HubConnectionBuilder()
.WithUrl(_connectionUrl)
.Build();
await _hub.StartAsync(); // ERROR
}
服务器:
public class LocationHub : ServerlessHub
{
private const string NewMessageTarget = "newMessage";
private const string NewConnectionTarget = "newConnection";
[FunctionName(nameof(Negotiate))]
public SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req)
{
return Negotiate(req.Headers["x-ms-signalr-user-id"], GetClaims(req.Headers["Authorization"]));
}
[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger] InvocationContext invocationContext, ILogger logger)
{
invocationContext.Headers.TryGetValue("Authorization", out var auth);
await Clients.All.SendAsync(NewConnectionTarget, new NewConnection(invocationContext.ConnectionId, auth));
logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}
[FunctionAuthorize]
[FunctionName(nameof(LocationUpdate))]
public async Task LocationUpdate([SignalRTrigger] InvocationContext invocationContext, SubjectLocation update, ILogger logger)
{
await Clients.All.SendAsync(NewMessageTarget, new NewMessage(invocationContext, update));
logger.LogInformation($"{invocationContext.ConnectionId} broadcast {update}");
}
class NewConnection
{
public string ConnectionId { get; }
public string Authentication { get; }
public NewConnection(string connectionId, string authentication)
{
ConnectionId = connectionId;
Authentication = authentication;
}
}
class NewMessage
{
public string ConnectionId { get; }
public string Sender { get; }
public SubjectLocation Update { get; }
public NewMessage(InvocationContext invocationContext, SubjectLocation update)
{
Sender = string.IsNullOrEmpty(invocationContext.UserId) ? string.Empty : invocationContext.UserId;
ConnectionId = invocationContext.ConnectionId;
Update = update;
}
}
}
}
附录:
解决方案
推荐阅读
- python - 使用循环从 NetCDF 生成多个底图
- python - Python - 刽子手
- php - 将数组作为 POST 请求参数发送到 PHP - Swift
- laravel - DELETE 方法在 PHP 变量中不起作用
- android - 我的 colors.xml 文件没有显示主题颜色。一切都是灰色的。如何解决这个问题?
- sapui5 - 使用 SAP UI5 动态呈现视图元素
- c# - 收到错误“Type Universe 无法解析程序集:System.Runtime,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a。”
- deployment - Sylius:“缓存:清除”超时
- r - 'ivreg' 代码(来自 AER 包)适用于较弱的 PC,但不适用于较强的 PC
- entity-framework-core - 实体框架核心数据迁移和在 EF 之外创建的表