|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using Microsoft.Azure.Functions.Worker; |
| 7 | +using Microsoft.Azure.Functions.Worker.Http; |
| 8 | + |
| 9 | +namespace SampleApp |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// The class is the same as SignalROutputBindingFunctions except the comments. Just keep the original one because the learn website refers to it. |
| 13 | + /// </summary> |
| 14 | + public static class SignalROutputBindingFunctions2 |
| 15 | + { |
| 16 | + // <snippet_broadcast_to_all> |
| 17 | + [Function("BroadcastToAll")] |
| 18 | + [SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnection")] |
| 19 | + public static SignalRMessageAction BroadcastToAll([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) |
| 20 | + { |
| 21 | + using var bodyReader = new StreamReader(req.Body); |
| 22 | + return new SignalRMessageAction("newMessage") |
| 23 | + { |
| 24 | + // broadcast to all the connected clients without specifying any connection, user or group. |
| 25 | + Arguments = new[] { bodyReader.ReadToEnd() }, |
| 26 | + }; |
| 27 | + } |
| 28 | + // </snippet_broadcast_to_all> |
| 29 | + |
| 30 | + // <snippet_send_to_connection> |
| 31 | + [Function("SendToConnection")] |
| 32 | + [SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnection")] |
| 33 | + public static SignalRMessageAction SendToConnection([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) |
| 34 | + { |
| 35 | + using var bodyReader = new StreamReader(req.Body); |
| 36 | + return new SignalRMessageAction("newMessage") |
| 37 | + { |
| 38 | + Arguments = new[] { bodyReader.ReadToEnd() }, |
| 39 | + ConnectionId = "connectionToSend", |
| 40 | + }; |
| 41 | + } |
| 42 | + // </snippet_send_to_connection> |
| 43 | + |
| 44 | + // <snippet_send_to_user> |
| 45 | + [Function("SendToUser")] |
| 46 | + [SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnection")] |
| 47 | + public static SignalRMessageAction SendToUser([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) |
| 48 | + { |
| 49 | + using var bodyReader = new StreamReader(req.Body); |
| 50 | + return new SignalRMessageAction("newMessage") |
| 51 | + { |
| 52 | + Arguments = new[] { bodyReader.ReadToEnd() }, |
| 53 | + UserId = "userToSend", |
| 54 | + }; |
| 55 | + } |
| 56 | + // </snippet_send_to_user> |
| 57 | + |
| 58 | + // <snippet_send_to_group> |
| 59 | + [Function("SendToGroup")] |
| 60 | + [SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnection")] |
| 61 | + public static SignalRMessageAction SendToGroup([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) |
| 62 | + { |
| 63 | + using var bodyReader = new StreamReader(req.Body); |
| 64 | + return new SignalRMessageAction("newMessage") |
| 65 | + { |
| 66 | + Arguments = new[] { bodyReader.ReadToEnd() }, |
| 67 | + GroupName = "groupToSend" |
| 68 | + }; |
| 69 | + } |
| 70 | + // </snippet_send_to_group> |
| 71 | + |
| 72 | + // <snippet_send_to_endpoint> |
| 73 | + [Function("SendToEndpoint")] |
| 74 | + [SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnection")] |
| 75 | + public static SignalRMessageAction SendToEndpoint( |
| 76 | + [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req, |
| 77 | + [SignalREndpointsInput("chat", ConnectionStringSetting = "SignalRConnection")] SignalREndpoint[] endpoints) |
| 78 | + { |
| 79 | + using var bodyReader = new StreamReader(req.Body); |
| 80 | + return new SignalRMessageAction("newMessage") |
| 81 | + { |
| 82 | + Arguments = new[] { bodyReader.ReadToEnd() }, |
| 83 | + // Only send to primary endpoint if you have configured multiple SignalR Service instances. |
| 84 | + // The use of 'Endpoints' can be combined with other properties such as UserId, GroupName, ConnectionID. |
| 85 | + Endpoints = endpoints.Where(e => e.EndpointType == SignalREndpointType.Primary).ToArray() |
| 86 | + }; |
| 87 | + } |
| 88 | + // </snippet_send_to_endpoint> |
| 89 | + |
| 90 | + // <snippet_remove_from_group> |
| 91 | + [Function("RemoveFromGroup")] |
| 92 | + [SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnection")] |
| 93 | + public static SignalRGroupAction RemoveFromGroup([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req) |
| 94 | + { |
| 95 | + return new SignalRGroupAction(SignalRGroupActionType.Remove) |
| 96 | + { |
| 97 | + GroupName = "group1", |
| 98 | + UserId = "user1" |
| 99 | + }; |
| 100 | + } |
| 101 | + // </snippet_remove_from_group> |
| 102 | + } |
| 103 | +} |
0 commit comments