Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Extensions to create a broadcaster from IGroup. #233

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions sandbox/Sandbox.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ static async Task Main(string[] args)
var result = await clientMyService.HelloAsync();
var result2 = await clientManagementService.FooBarAsync();

var clientHub = StreamingHubClient.Connect<IMyHub, IMyHubReceiver>(new Channel("localhost", 12345, creds), null);
var clientHub = StreamingHubClient.Connect<IMyHub, IMyHubReceiver>(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
Expand Down Expand Up @@ -144,14 +152,28 @@ public interface IMyHub : IStreamingHub<IMyHub, IMyHubReceiver>
{
Task<string> HelloAsync();
}

public interface IMyHubReceiver
{ }
{
void OnNantoka(string value);
}

public class MyHub : StreamingHubBase<IMyHub, IMyHubReceiver>, IMyHub
{
public Task<string> HelloAsync()
public async Task<string> HelloAsync()
{
return Task.FromResult("Konnnichiwa!");
var group = await this.Group.AddAsync("Nantoka");
group.CreateBroadcaster<IMyHubReceiver>().OnNantoka("BroadcastAll");
group.CreateBroadcasterTo<IMyHubReceiver>(Context.ContextId).OnNantoka("BroadcastTo(Self)");
group.CreateBroadcasterTo<IMyHubReceiver>(Guid.NewGuid()).OnNantoka("BroadcastTo(Non-self)");
group.CreateBroadcasterTo<IMyHubReceiver>(new[] { Guid.NewGuid(), Guid.NewGuid() }).OnNantoka("BroadcastTo(Non-self, Non-self)");
group.CreateBroadcasterTo<IMyHubReceiver>(new[] { Context.ContextId, Guid.NewGuid() }).OnNantoka("BroadcastTo(Self, Non-self)");
group.CreateBroadcasterExcept<IMyHubReceiver>(Context.ContextId).OnNantoka("BroadcastExcept(Self)");
group.CreateBroadcasterExcept<IMyHubReceiver>(Guid.NewGuid()).OnNantoka("BroadcastExcept(Non-self)");
group.CreateBroadcasterExcept<IMyHubReceiver>(new[] { Guid.NewGuid(), Guid.NewGuid() }).OnNantoka("BroadcastExcept(Non-self, Non-self)");
group.CreateBroadcasterExcept<IMyHubReceiver>(new[] { Context.ContextId, Guid.NewGuid() }).OnNantoka("BroadcastExcept(Self, Non-self)");

return "Konnnichiwa!";
}
}
}
67 changes: 67 additions & 0 deletions src/MagicOnion/Server/Hubs/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,71 @@ public void Remove(Guid id)
storage.TryRemove(id, out _);
}
}

public static class GroupBroadcastExtensions
{
/// <summary>
/// Create a receiver proxy from the group. Can be use to broadcast messages to all clients.
/// </summary>
/// <typeparam name="TReceiver"></typeparam>
/// <param name="group"></param>
/// <returns></returns>
public static TReceiver CreateBroadcaster<TReceiver>(this IGroup group)
{
var type = DynamicBroadcasterBuilder<TReceiver>.BroadcasterType;
return (TReceiver) Activator.CreateInstance(type, group);
}

/// <summary>
/// Create a receiver proxy from the group. Can be use to broadcast messages to all clients excepts one.
/// </summary>
/// <typeparam name="TReceiver"></typeparam>
/// <param name="group"></param>
/// <param name="except"></param>
/// <returns></returns>
public static TReceiver CreateBroadcasterExcept<TReceiver>(this IGroup group, Guid except)
{
var type = DynamicBroadcasterBuilder<TReceiver>.BroadcasterType_ExceptOne;
return (TReceiver) Activator.CreateInstance(type, new object[] {group, except});
}

/// <summary>
/// Create a receiver proxy from the group. Can be use to broadcast messages to all clients excepts some clients.
/// </summary>
/// <typeparam name="TReceiver"></typeparam>
/// <param name="group"></param>
/// <param name="excepts"></param>
/// <returns></returns>
public static TReceiver CreateBroadcasterExcept<TReceiver>(this IGroup group, Guid[] excepts)
{
var type = DynamicBroadcasterBuilder<TReceiver>.BroadcasterType_ExceptMany;
return (TReceiver) Activator.CreateInstance(type, new object[] {group, excepts});
}

/// <summary>
/// Create a receiver proxy from the group. Can be use to broadcast messages to one client.
/// </summary>
/// <typeparam name="TReceiver"></typeparam>
/// <param name="group"></param>
/// <param name="toConnectionId"></param>
/// <returns></returns>
public static TReceiver CreateBroadcasterTo<TReceiver>(this IGroup group, Guid toConnectionId)
{
var type = DynamicBroadcasterBuilder<TReceiver>.BroadcasterType_ToOne;
return (TReceiver) Activator.CreateInstance(type, new object[] { group, toConnectionId });
}

/// <summary>
/// Create a receiver proxy from the group. Can be use to broadcast messages to some clients.
/// </summary>
/// <typeparam name="TReceiver"></typeparam>
/// <param name="group"></param>
/// <param name="toConnectionIds"></param>
/// <returns></returns>
public static TReceiver CreateBroadcasterTo<TReceiver>(this IGroup group, Guid[] toConnectionIds)
{
var type = DynamicBroadcasterBuilder<TReceiver>.BroadcasterType_ToMany;
return (TReceiver) Activator.CreateInstance(type, new object[] { group, toConnectionIds });
}
}
}