所以我在这里学习了几个教程:https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-quickstart-azure-functions-csharp和https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-concept-serverless-development-config
我的问题是,所有的示例都显示了一个CRON操作,或者只是一个作用域内的控制台日志广播。但是我希望通过更新集合来触发广播吗?
在Web应用程序中,我可以这样做。当使用多个客户端进行测试时,这种方法工作得很好。
[HttpPost]
public async Task<IActionResult> Insert([FromQuery] string firstName, string lastName)
{
await _repo.InsertNewPerson(firstName, lastName);
//This hub essentially has almost nothing set up except:
// endpoints.MapHub<ContactsHub>("/contacts"); setup in startup
await _hub.Clients.All.SendAsync("transfercontactdata", _repo.GetContacts());
return StatusCode(201);
}然而,在一个Azure函数中,我看到了下面这段代码:使用微软的预制类添加一个'Broadcast‘SignalRTrigger
public class SignalRTestHub : ServerlessHub
{
private IContactsRepo _repo = new ContactsRepo();
[FunctionName("negotiate")]
public SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, [SignalRConnectionInfo(HubName = "SignalRTestHub")] SignalRConnectionInfo connectionInfo)
{
return connectionInfo;
}
//Brett: Do I need this arg more in other events to ensure this acts as a load balancer??? ... ran out of time, [SignalRConnectionInfo(HubName = "SignalRTestHub")] SignalRConnectionInfo connectionInfo
[FunctionName(nameof(OnConnected))]
public async Task OnConnected([SignalRTrigger] InvocationContext invocationContext, ILogger logger, [SignalRConnectionInfo(HubName = "SignalRTestHub")] SignalRConnectionInfo connectionInfo)
{
//await Clients.All.SendAsync(NewConnectionTarget, new NewConnection(invocationContext.ConnectionId));
logger.LogInformation($"{invocationContext.ConnectionId} has connected");
}
[FunctionName(nameof(Broadcast))]
public async Task Broadcast([SignalRTrigger] InvocationContext invocationContext, string message, ILogger logger)
{
//Broadcast on update the same as the get call on the other function
await Clients.All.SendAsync("getting data", await _repo.GetContacts());
logger.LogInformation($"{invocationContext.ConnectionId} broadcast {message}");
}
[FunctionName(nameof(OnDisconnected))]
public void OnDisconnected([SignalRTrigger] InvocationContext invocationContext)
{
}
}然后,我有另一个HTTP触发器函数,如下所示:
public static class SignalRTesting
{
private static IContactsRepo _repo = new ContactsRepo();
private static SignalRTestHub _hub = new SignalRTestHub();
private static SignalRConnectionInfo _info;
static SignalRTesting()
{
//Not sure how connect or need to
_info = _hub.Negotiate(null, null);
}
[FunctionName("GetData")]
public static async Task<IActionResult> GetData([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) => new OkObjectResult(await _repo.GetContacts());
[FunctionName("UpdateData")]
public static async Task<IActionResult> UpdateData([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var data = JsonConvert.DeserializeObject<Contact>(requestBody);
await _repo.InsertNewPerson(data.FirstName, data.LastName);
await _hub.Broadcast() //I need a context from above I am guessing
return new NoContentResult();
}
}所以我的问题是,如何获得HTTP POST或PUT请求,以便使用函数调用广播?在我看来,集线器广播需要一个连接,所以几乎就像有一个服务端充当客户端一样,但我不确定。
发布于 2021-08-26 18:13:37
在Azure函数中,您只需利用SignalR Service Output Binding向所有客户端广播即可。其余的由底层的库来处理。
https://stackoverflow.com/questions/68657511
复制相似问题