From c56ccc19ef9e9ad075cda4803a1654d6bc8b7fac Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Thu, 28 Nov 2019 12:02:33 +0900 Subject: [PATCH] feat: Add Extensions to create a broadcaster from IGroup. --- sandbox/Sandbox.Hosting/Program.cs | 30 +++++++++++-- src/MagicOnion/Server/Hubs/Group.cs | 67 +++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/sandbox/Sandbox.Hosting/Program.cs b/sandbox/Sandbox.Hosting/Program.cs index e094f2985..58c75454a 100644 --- a/sandbox/Sandbox.Hosting/Program.cs +++ b/sandbox/Sandbox.Hosting/Program.cs @@ -60,11 +60,19 @@ static async Task Main(string[] args) var result = await clientMyService.HelloAsync(); var result2 = await clientManagementService.FooBarAsync(); - var clientHub = StreamingHubClient.Connect(new Channel("localhost", 12345, creds), null); + var clientHub = StreamingHubClient.Connect(new Channel("localhost", 12345, creds), new MyHubReceiver()); var result3 = await clientHub.HelloAsync(); await hostTask; } + + class MyHubReceiver : IMyHubReceiver + { + public void OnNantoka(string value) + { + Console.WriteLine(value); + } + } } public class MyStreamingHubFilterAttribute : StreamingHubFilterAttribute @@ -144,14 +152,28 @@ public interface IMyHub : IStreamingHub { Task HelloAsync(); } + public interface IMyHubReceiver - { } + { + void OnNantoka(string value); + } public class MyHub : StreamingHubBase, IMyHub { - public Task HelloAsync() + public async Task HelloAsync() { - return Task.FromResult("Konnnichiwa!"); + var group = await this.Group.AddAsync("Nantoka"); + group.CreateBroadcaster().OnNantoka("BroadcastAll"); + group.CreateBroadcasterTo(Context.ContextId).OnNantoka("BroadcastTo(Self)"); + group.CreateBroadcasterTo(Guid.NewGuid()).OnNantoka("BroadcastTo(Non-self)"); + group.CreateBroadcasterTo(new[] { Guid.NewGuid(), Guid.NewGuid() }).OnNantoka("BroadcastTo(Non-self, Non-self)"); + group.CreateBroadcasterTo(new[] { Context.ContextId, Guid.NewGuid() }).OnNantoka("BroadcastTo(Self, Non-self)"); + group.CreateBroadcasterExcept(Context.ContextId).OnNantoka("BroadcastExcept(Self)"); + group.CreateBroadcasterExcept(Guid.NewGuid()).OnNantoka("BroadcastExcept(Non-self)"); + group.CreateBroadcasterExcept(new[] { Guid.NewGuid(), Guid.NewGuid() }).OnNantoka("BroadcastExcept(Non-self, Non-self)"); + group.CreateBroadcasterExcept(new[] { Context.ContextId, Guid.NewGuid() }).OnNantoka("BroadcastExcept(Self, Non-self)"); + + return "Konnnichiwa!"; } } } diff --git a/src/MagicOnion/Server/Hubs/Group.cs b/src/MagicOnion/Server/Hubs/Group.cs index 1e1327fb8..b09429f34 100644 --- a/src/MagicOnion/Server/Hubs/Group.cs +++ b/src/MagicOnion/Server/Hubs/Group.cs @@ -207,4 +207,71 @@ public void Remove(Guid id) storage.TryRemove(id, out _); } } + + public static class GroupBroadcastExtensions + { + /// + /// Create a receiver proxy from the group. Can be use to broadcast messages to all clients. + /// + /// + /// + /// + public static TReceiver CreateBroadcaster(this IGroup group) + { + var type = DynamicBroadcasterBuilder.BroadcasterType; + return (TReceiver) Activator.CreateInstance(type, group); + } + + /// + /// Create a receiver proxy from the group. Can be use to broadcast messages to all clients excepts one. + /// + /// + /// + /// + /// + public static TReceiver CreateBroadcasterExcept(this IGroup group, Guid except) + { + var type = DynamicBroadcasterBuilder.BroadcasterType_ExceptOne; + return (TReceiver) Activator.CreateInstance(type, new object[] {group, except}); + } + + /// + /// Create a receiver proxy from the group. Can be use to broadcast messages to all clients excepts some clients. + /// + /// + /// + /// + /// + public static TReceiver CreateBroadcasterExcept(this IGroup group, Guid[] excepts) + { + var type = DynamicBroadcasterBuilder.BroadcasterType_ExceptMany; + return (TReceiver) Activator.CreateInstance(type, new object[] {group, excepts}); + } + + /// + /// Create a receiver proxy from the group. Can be use to broadcast messages to one client. + /// + /// + /// + /// + /// + public static TReceiver CreateBroadcasterTo(this IGroup group, Guid toConnectionId) + { + var type = DynamicBroadcasterBuilder.BroadcasterType_ToOne; + return (TReceiver) Activator.CreateInstance(type, new object[] { group, toConnectionId }); + } + + /// + /// Create a receiver proxy from the group. Can be use to broadcast messages to some clients. + /// + /// + /// + /// + /// + public static TReceiver CreateBroadcasterTo(this IGroup group, Guid[] toConnectionIds) + { + var type = DynamicBroadcasterBuilder.BroadcasterType_ToMany; + return (TReceiver) Activator.CreateInstance(type, new object[] { group, toConnectionIds }); + } + } }