Updating SocketsSample with new IHubClients and IHubCallerClients APIs (#1282)

This commit is contained in:
Mikael Mengistu 2018-01-10 12:30:25 -08:00 committed by GitHub
parent 89b532c985
commit 50fe88195e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 25 deletions

View File

@ -17,7 +17,7 @@ namespace SocketsSample.Hubs
public override async Task OnDisconnectedAsync(Exception ex) 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) public Task Send(string message)
@ -25,11 +25,21 @@ namespace SocketsSample.Hubs
return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}"); 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) public Task SendToGroup(string groupName, string message)
{ {
return Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId}@{groupName}: {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) public async Task JoinGroup(string groupName)
{ {
await Groups.AddAsync(Context.ConnectionId, groupName); await Groups.AddAsync(Context.ConnectionId, groupName);
@ -39,14 +49,14 @@ namespace SocketsSample.Hubs
public async Task LeaveGroup(string groupName) public async Task LeaveGroup(string groupName)
{ {
await Groups.RemoveAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId} left {groupName}"); await Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId} left {groupName}");
await Groups.RemoveAsync(Context.ConnectionId, groupName);
} }
public Task Echo(string message) public Task Echo(string message)
{ {
return Clients.Client(Context.ConnectionId).InvokeAsync("Send", $"{Context.ConnectionId}: {message}"); return Clients.Caller.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
} }
} }
} }

View File

@ -16,7 +16,7 @@ namespace SocketsSample.Hubs
public override async Task OnDisconnectedAsync(Exception ex) 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) public Task Send(string message)
@ -24,11 +24,21 @@ namespace SocketsSample.Hubs
return Clients.All.Send($"{Context.ConnectionId}: {message}"); 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) public Task SendToGroup(string groupName, string message)
{ {
return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {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) public async Task JoinGroup(string groupName)
{ {
await Groups.AddAsync(Context.ConnectionId, groupName); await Groups.AddAsync(Context.ConnectionId, groupName);
@ -38,14 +48,14 @@ namespace SocketsSample.Hubs
public async Task LeaveGroup(string groupName) public async Task LeaveGroup(string groupName)
{ {
await Groups.RemoveAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}"); await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}");
await Groups.RemoveAsync(Context.ConnectionId, groupName);
} }
public Task Echo(string message) public Task Echo(string message)
{ {
return Clients.Client(Context.ConnectionId).Send($"{Context.ConnectionId}: {message}"); return Clients.Caller.Send($"{Context.ConnectionId}: {message}");
} }
} }
} }

View File

@ -16,7 +16,7 @@ namespace SocketsSample.Hubs
public override async Task OnDisconnectedAsync(Exception ex) 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) public Task Send(string message)
@ -24,11 +24,21 @@ namespace SocketsSample.Hubs
return Clients.All.Send($"{Context.ConnectionId}: {message}"); 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) public Task SendToGroup(string groupName, string message)
{ {
return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {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) public async Task JoinGroup(string groupName)
{ {
await Groups.AddAsync(Context.ConnectionId, groupName); await Groups.AddAsync(Context.ConnectionId, groupName);
@ -38,14 +48,14 @@ namespace SocketsSample.Hubs
public async Task LeaveGroup(string groupName) public async Task LeaveGroup(string groupName)
{ {
await Groups.RemoveAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}"); await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}");
await Groups.RemoveAsync(Context.ConnectionId, groupName);
} }
public Task Echo(string message) public Task Echo(string message)
{ {
return Clients.Client(Context.ConnectionId).Send($"{Context.ConnectionId}: {message}"); return Clients.Caller.Send($"{Context.ConnectionId}: {message}");
} }
} }

View File

@ -1,4 +1,4 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@ -24,10 +24,9 @@
<div class="input-append"> <div class="input-append">
<input type="text" id="message-text" placeholder="Type a message, name or group" /> <input type="text" id="message-text" placeholder="Type a message, name or group" />
<input type="button" id="broadcast" class="btn" value="Broadcast" /> <input type="button" id="broadcast" class="btn" value="Broadcast" />
<input type="button" id="broadcast-exceptme" class="btn" value="Broadcast (All Except Me)" /> <input type="button" id="broadcast-exceptme" class="btn" value="Broadcast (Others)" />
<input type="button" id="join" class="btn" value="Enter Name" /> <input type="button" id="join" class="btn" value="Enter Name" />
<input type="button" id="join-group" class="btn" value="Join Group" />
<input type="button" id="leave-group" class="btn" value="Leave Group" />
</div> </div>
</form> </form>
@ -35,16 +34,20 @@
<form class="form-inline"> <form class="form-inline">
<div class="input-append"> <div class="input-append">
<input type="text" id="me-message-text" placeholder="Type a message" /> <input type="text" id="me-message-text" placeholder="Type a message" />
<input type="button" id="send" class="btn" value="Send to me" /> <input type="button" id="send" class="btn" value="Send to Me" />
</div> </div>
</form> </form>
<h4>Private Message</h4> <h4>Group Actions</h4>
<form class="form-inline"> <form class="form-inline">
<div class="input-prepend input-append"> <div class="input-prepend input-append">
<input type="text" name="private-message" id="private-message-text" placeholder="Type a message" /> <input type="text" name="group-message" id="group-message-text" placeholder="Type a message" />
<input type="text" name="user" id="target" placeholder="Type a group name" /> <input type="text" name="group-name" id="group-name" placeholder="Type a group name" />
<input type="button" id="groupmsg" class="btn" value="Send to group" /> <input type="button" id="groupmsg" class="btn" value="Send to Group" />
<input type="button" id="others-groupmsg" class="btn" value="Send to Others in Group" />
<input type="button" id="join-group" class="btn" value="Join Group" />
<input type="button" id="leave-group" class="btn" value="Leave Group" />
</div> </div>
</form> </form>
@ -144,24 +147,35 @@
}); });
click('join-group', function (event) { click('join-group', function (event) {
let groupName = getText('message-text'); let groupName = getText('group-name');
invoke(connection, 'JoinGroup', groupName); invoke(connection, 'JoinGroup', groupName);
}); });
click('leave-group', function (event) { click('leave-group', function (event) {
let groupName = getText('message-text'); let groupName = getText('group-name');
invoke(connection, 'LeaveGroup', groupName); invoke(connection, 'LeaveGroup', groupName);
}); });
click('groupmsg', function (event) { click('groupmsg', function (event) {
let groupName = getText('target'); let groupName = getText('group-name');
let message = getText('private-message-text'); let message = getText('group-message-text');
invoke(connection, 'SendToGroup', groupName, message); 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) { click('send', function (event) {
let data = getText('me-message-text'); let data = getText('me-message-text');
invoke(connection, 'Echo', data); invoke(connection, 'Echo', data);
}); });
click('broadcast-exceptme', function (event) {
let data = getText('message-text');
invoke(connection, 'SendToOthers', data);
});
</script> </script>