From 50fe88195e00d33620db37a6a10339b8e0b5f47b Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Wed, 10 Jan 2018 12:30:25 -0800 Subject: [PATCH] Updating SocketsSample with new IHubClients and IHubCallerClients APIs (#1282) --- samples/SocketsSample/Hubs/Chat.cs | 18 +++++++--- samples/SocketsSample/Hubs/DynamicChat.cs | 18 +++++++--- samples/SocketsSample/Hubs/HubTChat.cs | 18 +++++++--- samples/SocketsSample/wwwroot/hubs.html | 40 +++++++++++++++-------- 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/samples/SocketsSample/Hubs/Chat.cs b/samples/SocketsSample/Hubs/Chat.cs index a01e1dec00..64849963f0 100644 --- a/samples/SocketsSample/Hubs/Chat.cs +++ b/samples/SocketsSample/Hubs/Chat.cs @@ -17,7 +17,7 @@ namespace SocketsSample.Hubs public override async Task OnDisconnectedAsync(Exception ex) { - await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} left"); + await Clients.Others.InvokeAsync("Send", $"{Context.ConnectionId} left"); } public Task Send(string message) @@ -25,11 +25,21 @@ namespace SocketsSample.Hubs return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}"); } + public Task SendToOthers(string message) + { + return Clients.Others.InvokeAsync("Send", $"{Context.ConnectionId}: {message}"); + } + public Task SendToGroup(string groupName, string message) { return Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}"); } + public Task SendToOthersInGroup(string groupName, string message) + { + return Clients.OthersInGroup(groupName).InvokeAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}"); + } + public async Task JoinGroup(string groupName) { await Groups.AddAsync(Context.ConnectionId, groupName); @@ -39,14 +49,14 @@ namespace SocketsSample.Hubs public async Task LeaveGroup(string groupName) { - await Groups.RemoveAsync(Context.ConnectionId, groupName); - await Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId} left {groupName}"); + + await Groups.RemoveAsync(Context.ConnectionId, groupName); } public Task Echo(string message) { - return Clients.Client(Context.ConnectionId).InvokeAsync("Send", $"{Context.ConnectionId}: {message}"); + return Clients.Caller.InvokeAsync("Send", $"{Context.ConnectionId}: {message}"); } } } diff --git a/samples/SocketsSample/Hubs/DynamicChat.cs b/samples/SocketsSample/Hubs/DynamicChat.cs index 5966a7267d..e1b11956c8 100644 --- a/samples/SocketsSample/Hubs/DynamicChat.cs +++ b/samples/SocketsSample/Hubs/DynamicChat.cs @@ -16,7 +16,7 @@ namespace SocketsSample.Hubs public override async Task OnDisconnectedAsync(Exception ex) { - await Clients.All.Send($"{Context.ConnectionId} left"); + await Clients.Others.Send($"{Context.ConnectionId} left"); } public Task Send(string message) @@ -24,11 +24,21 @@ namespace SocketsSample.Hubs return Clients.All.Send($"{Context.ConnectionId}: {message}"); } + public Task SendToOthers(string message) + { + return Clients.Others.Send($"{Context.ConnectionId}: {message}"); + } + public Task SendToGroup(string groupName, string message) { return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}"); } + public Task SendToOthersInGroup(string groupName, string message) + { + return Clients.OthersInGroup(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}"); + } + public async Task JoinGroup(string groupName) { await Groups.AddAsync(Context.ConnectionId, groupName); @@ -38,14 +48,14 @@ namespace SocketsSample.Hubs public async Task LeaveGroup(string groupName) { - await Groups.RemoveAsync(Context.ConnectionId, groupName); - await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}"); + + await Groups.RemoveAsync(Context.ConnectionId, groupName); } public Task Echo(string message) { - return Clients.Client(Context.ConnectionId).Send($"{Context.ConnectionId}: {message}"); + return Clients.Caller.Send($"{Context.ConnectionId}: {message}"); } } } diff --git a/samples/SocketsSample/Hubs/HubTChat.cs b/samples/SocketsSample/Hubs/HubTChat.cs index c96b3867e4..b352bf7747 100644 --- a/samples/SocketsSample/Hubs/HubTChat.cs +++ b/samples/SocketsSample/Hubs/HubTChat.cs @@ -16,7 +16,7 @@ namespace SocketsSample.Hubs public override async Task OnDisconnectedAsync(Exception ex) { - await Clients.All.Send($"{Context.ConnectionId} left"); + await Clients.Others.Send($"{Context.ConnectionId} left"); } public Task Send(string message) @@ -24,11 +24,21 @@ namespace SocketsSample.Hubs return Clients.All.Send($"{Context.ConnectionId}: {message}"); } + public Task SendToOthers(string message) + { + return Clients.Others.Send($"{Context.ConnectionId}: {message}"); + } + public Task SendToGroup(string groupName, string message) { return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}"); } + public Task SendToOthersInGroup(string groupName, string message) + { + return Clients.OthersInGroup(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}"); + } + public async Task JoinGroup(string groupName) { await Groups.AddAsync(Context.ConnectionId, groupName); @@ -38,14 +48,14 @@ namespace SocketsSample.Hubs public async Task LeaveGroup(string groupName) { - await Groups.RemoveAsync(Context.ConnectionId, groupName); - await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}"); + + await Groups.RemoveAsync(Context.ConnectionId, groupName); } public Task Echo(string message) { - return Clients.Client(Context.ConnectionId).Send($"{Context.ConnectionId}: {message}"); + return Clients.Caller.Send($"{Context.ConnectionId}: {message}"); } } diff --git a/samples/SocketsSample/wwwroot/hubs.html b/samples/SocketsSample/wwwroot/hubs.html index 451950b3c1..30c069709b 100644 --- a/samples/SocketsSample/wwwroot/hubs.html +++ b/samples/SocketsSample/wwwroot/hubs.html @@ -1,4 +1,4 @@ - + @@ -24,10 +24,9 @@
- + - - +
@@ -35,16 +34,20 @@
- +
-

Private Message

+

Group Actions

- - - + + + + + + +
@@ -144,24 +147,35 @@ }); click('join-group', function (event) { - let groupName = getText('message-text'); + let groupName = getText('group-name'); invoke(connection, 'JoinGroup', groupName); }); click('leave-group', function (event) { - let groupName = getText('message-text'); + let groupName = getText('group-name'); invoke(connection, 'LeaveGroup', groupName); }); click('groupmsg', function (event) { - let groupName = getText('target'); - let message = getText('private-message-text'); + let groupName = getText('group-name'); + let message = getText('group-message-text'); invoke(connection, 'SendToGroup', groupName, message); }); + click('others-groupmsg', function (event) { + let groupName = getText('group-name'); + let message = getText('group-message-text'); + invoke(connection, 'SendToOthersInGroup', groupName, message); + }); + click('send', function (event) { let data = getText('me-message-text'); invoke(connection, 'Echo', data); }); + click('broadcast-exceptme', function (event) { + let data = getText('message-text'); + invoke(connection, 'SendToOthers', data); + }); +