【发布时间】:2023-03-10 08:40:01
【问题描述】:
我的目标是:
- 在计划函数中 - 向 SignalR 添加消息
- 在 SPA 应用程序 (vue.js) 中订阅事件并调用 API 以更新视图
目前,我正在尝试在我的 Function 应用程序(隔离,.net 6.0)中向 SignalR 获取任何东西。
我在函数应用中拥有的东西:
[Function("negotiate")]
public HttpResponseData Negotiate(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
[SignalRConnectionInfoInput(HubName = "AdminHub", ConnectionStringSetting = "AzureSignalRConnectionString")] SignalRConnectionInfo connectionInfo)
{
_logger.LogInformation($"SignalR Connection URL = '{connectionInfo.Url}'");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString($"Connection URL = '{connectionInfo.Url}'");
return response;
}
}
[Function("SendMessage")]
[SignalROutput(HubName = "AdminHub", ConnectionStringSetting = "AzureSignalRConnectionString")]
public SignalRMessage SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] Microsoft.Azure.Functions.Worker.Http.HttpRequestData req)
{
return
new SignalRMessage
{
Target = "cancelToHandle",
MethodName = "cancelToHandle",
Arguments = new[] { "hello" }
};
}
[Function("SignalRTest")]
public static async Task SignalRTest([SignalRTrigger("AdminHub", "messages", "cancelToHandle", ConnectionStringSetting = "AzureSignalRConnectionString")] string message, ILogger logger)
{
logger.LogInformation($"Receive {message}.");
}
没有调用协商函数。什么时候调用?
如果我调用 SendMessage,没有错误,但 SignalR 服务没有任何反应。我应该在那里看到连接和消息吗? (目前指标为零)。
我尝试创建一个测试“模拟器”客户端 - 只是一个控制台应用程序:
var url = "http://<azureSignalRUrl>/AdminHub";
var connection = new HubConnectionBuilder()
.WithUrl(url)
.WithAutomaticReconnect()
.Build();
// receive a message from the hub
connection.On<string, string>("cancelToHandle", (user, message) => OnReceiveMessage(user, message));
await connection.StartAsync();
// send a message to the hub
await connection.InvokeAsync("SendMessage", "ConsoleApp", "Message from the console app");
void OnReceiveMessage(string user, string message)
{
Console.WriteLine($"{user}: {message}");
}
并抛出异常“:'连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应。(:80)'
我认为我对应该发生的事情缺乏整体理解:
- 何时触发协商功能
- 我可以查看我在 Azure 门户(在 SignalR 服务中)发送的消息吗?
- 如何在测试中轻松接收它们
- 参数/属性是什么意思(目标/方法名称/类别)。例子: SignalRTriggerAttribute 具有以下构造函数 public SignalRTriggerAttribute(string hubName, string category, string @event, params string[] parameterNames); 并且输出绑定接收我创建的任何自定义模型?
- 应在 SignalR 服务中设置哪些设置 - 现在我将其设置为无服务器模式 + CORS
【问题讨论】:
标签: azure azure-functions signalr .net-6.0