Skip to content

Instantly share code, notes, and snippets.

@GrillPhil
Last active May 23, 2018 08:22
Show Gist options
  • Save GrillPhil/6ddeaaa47649443b21fb03e31d5eb25e to your computer and use it in GitHub Desktop.
Save GrillPhil/6ddeaaa47649443b21fb03e31d5eb25e to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using Microsoft.Azure.WebJobs.Host;
using System.IO;
using System.Threading.Tasks;
namespace ServerlessRealtimeDemo
{
public static class MessageFunction
{
[FunctionName("message")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req,
[SignalR(HubName = "broadcast")]IAsyncCollector<SignalRMessage> signalRMessages,
TraceWriter log)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
if (string.IsNullOrEmpty(requestBody))
{
return new BadRequestObjectResult("Please pass a payload to broadcast in the request body.");
}
await signalRMessages.AddAsync(new SignalRMessage()
{
Target = "notify",
Arguments = new object[] { requestBody }
});
return new OkResult();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment