Skip to content

Update SignalR Sample #13078

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

Merged
merged 3 commits into from
Aug 13, 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
42 changes: 22 additions & 20 deletions src/SignalR/samples/SignalRSamples/Hubs/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,60 @@ namespace SignalRSamples.Hubs
{
public class Chat : Hub
{
public override async Task OnConnectedAsync()
public override Task OnConnectedAsync()
{
await Clients.All.SendAsync("Send", $"{Context.ConnectionId} joined");
var name = Context.GetHttpContext().Request.Query["name"];
return Clients.All.SendAsync("Send", $"{name} joined the chat");
}

public override async Task OnDisconnectedAsync(Exception ex)
public override Task OnDisconnectedAsync(Exception exception)
{
await Clients.Others.SendAsync("Send", $"{Context.ConnectionId} left");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include the user name in the query string or something so we can keep the user joined/left messages?

var name = Context.GetHttpContext().Request.Query["name"];
return Clients.All.SendAsync("Send", $"{name} left the chat");
}

public Task Send(string message)
public Task Send(string name, string message)
{
return Clients.All.SendAsync("Send", $"{Context.ConnectionId}: {message}");
return Clients.All.SendAsync("Send", $"{name}: {message}");
}

public Task SendToOthers(string message)
public Task SendToOthers(string name, string message)
{
return Clients.Others.SendAsync("Send", $"{Context.ConnectionId}: {message}");
return Clients.Others.SendAsync("Send", $"{name}: {message}");
}

public Task SendToConnection(string connectionId, string message)
public Task SendToConnection(string connectionId, string name, string message)
{
return Clients.Client(connectionId).SendAsync("Send", $"Private message from {Context.ConnectionId}: {message}");
return Clients.Client(connectionId).SendAsync("Send", $"Private message from {name}: {message}");
}

public Task SendToGroup(string groupName, string message)
public Task SendToGroup(string groupName, string name ,string message)
{
return Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
return Clients.Group(groupName).SendAsync("Send", $"{name}@{groupName}: {message}");
}

public Task SendToOthersInGroup(string groupName, string message)
public Task SendToOthersInGroup(string groupName, string name,string message)
{
return Clients.OthersInGroup(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
return Clients.OthersInGroup(groupName).SendAsync("Send", $"{name}@{groupName}: {message}");
}

public async Task JoinGroup(string groupName)
public async Task JoinGroup(string groupName, string name)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);

await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} joined {groupName}");
await Clients.Group(groupName).SendAsync("Send", $"{name} joined {groupName}");
}

public async Task LeaveGroup(string groupName)
public async Task LeaveGroup(string groupName, string name)
{
await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} left {groupName}");
await Clients.Group(groupName).SendAsync("Send", $"{name} left {groupName}");

await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
}

public Task Echo(string message)
public Task Echo(string name, string message)
{
return Clients.Caller.SendAsync("Send", $"{Context.ConnectionId}: {message}");
return Clients.Caller.SendAsync("Send", $"{name}: {message}");
}
}
}
38 changes: 20 additions & 18 deletions src/SignalR/samples/SignalRSamples/Hubs/DynamicChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,55 @@ namespace SignalRSamples.Hubs
{
public class DynamicChat : DynamicHub
{
public override async Task OnConnectedAsync()
public override Task OnConnectedAsync()
{
await Clients.All.Send($"{Context.ConnectionId} joined");
var name = Context.GetHttpContext().Request.Query["name"];
return Clients.All.Send($"{name} joined the chat");
}

public override async Task OnDisconnectedAsync(Exception ex)
public override Task OnDisconnectedAsync(Exception exception)
{
await Clients.Others.Send($"{Context.ConnectionId} left");
var name = Context.GetHttpContext().Request.Query["name"];
return Clients.All.Send($"{name} left the chat");
}

public Task Send(string message)
public Task Send(string name, string message)
{
return Clients.All.Send($"{Context.ConnectionId}: {message}");
return Clients.All.Send($"{name}: {message}");
}

public Task SendToOthers(string message)
public Task SendToOthers(string name, string message)
{
return Clients.Others.Send($"{Context.ConnectionId}: {message}");
return Clients.Others.Send($"{name}: {message}");
}

public Task SendToGroup(string groupName, string message)
public Task SendToGroup(string groupName, string name, string message)
{
return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
return Clients.Group(groupName).Send($"{name}@{groupName}: {message}");
}

public Task SendToOthersInGroup(string groupName, string message)
public Task SendToOthersInGroup(string groupName, string name, string message)
{
return Clients.OthersInGroup(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
return Clients.OthersInGroup(groupName).Send($"{name}@{groupName}: {message}");
}

public async Task JoinGroup(string groupName)
public async Task JoinGroup(string groupName, string name)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);

await Clients.Group(groupName).Send($"{Context.ConnectionId} joined {groupName}");
await Clients.Group(groupName).Send($"{name} joined {groupName}");
}

public async Task LeaveGroup(string groupName)
public async Task LeaveGroup(string groupName, string name)
{
await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}");
await Clients.Group(groupName).Send($"{name} left {groupName}");

await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
}

public Task Echo(string message)
public Task Echo(string name, string message)
{
return Clients.Caller.Send($"{Context.ConnectionId}: {message}");
return Clients.Caller.Send($"{name}: {message}");
}
}
}
38 changes: 20 additions & 18 deletions src/SignalR/samples/SignalRSamples/Hubs/HubTChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,55 @@ namespace SignalRSamples.Hubs
{
public class HubTChat : Hub<IChatClient>
{
public override async Task OnConnectedAsync()
public override Task OnConnectedAsync()
{
await Clients.All.Send($"{Context.ConnectionId} joined");
var name = Context.GetHttpContext().Request.Query["name"];
return Clients.All.Send($"{name} joined the chat");
}

public override async Task OnDisconnectedAsync(Exception ex)
public override Task OnDisconnectedAsync(Exception exception)
{
await Clients.Others.Send($"{Context.ConnectionId} left");
var name = Context.GetHttpContext().Request.Query["name"];
return Clients.All.Send($"{name} left the chat");
}

public Task Send(string message)
public Task Send(string name, string message)
{
return Clients.All.Send($"{Context.ConnectionId}: {message}");
return Clients.All.Send($"{name}: {message}");
}

public Task SendToOthers(string message)
public Task SendToOthers(string name, string message)
{
return Clients.Others.Send($"{Context.ConnectionId}: {message}");
return Clients.Others.Send($"{name}: {message}");
}

public Task SendToGroup(string groupName, string message)
public Task SendToGroup(string groupName, string name, string message)
{
return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
return Clients.Group(groupName).Send($"{name}@{groupName}: {message}");
}

public Task SendToOthersInGroup(string groupName, string message)
public Task SendToOthersInGroup(string groupName, string name, string message)
{
return Clients.OthersInGroup(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
return Clients.OthersInGroup(groupName).Send($"{name}@{groupName}: {message}");
}

public async Task JoinGroup(string groupName)
public async Task JoinGroup(string groupName, string name)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);

await Clients.Group(groupName).Send($"{Context.ConnectionId} joined {groupName}");
await Clients.Group(groupName).Send($"{name} joined {groupName}");
}

public async Task LeaveGroup(string groupName)
public async Task LeaveGroup(string groupName, string name)
{
await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}");
await Clients.Group(groupName).Send($"{name} left {groupName}");

await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
}

public Task Echo(string message)
public Task Echo(string name, string message)
{
return Clients.Caller.Send($"{Context.ConnectionId}: {message}");
return Clients.Caller.Send($"{name}: {message}");
}
}

Expand Down
26 changes: 18 additions & 8 deletions src/SignalR/samples/SignalRSamples/wwwroot/hubs.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1 id="head1">SignalR Hubs Sample</h1>
<option value="hubT">Hub&lt;T&gt;</option>
</select>

<input type="text" id="displayname" placeholder="Enter User Name" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss D'Artagnan 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same! ☹️

<input type="button" id="connect" value="Connect" />
<input type="button" id="disconnect" value="Disconnect" />

Expand Down Expand Up @@ -58,6 +59,7 @@ <h4>To Me</h4>
<div class="input-append">
<input type="text" id="me-message-text" placeholder="Type a message" />
<input type="button" id="send" class="btn" value="Send to Me" />
<input type="hidden" id="displayname" />
</div>
</form>

Expand Down Expand Up @@ -138,6 +140,13 @@ <h4>Group Actions</h4>
var connection;

click('connect', function (event) {

name = document.getElementById("displayname").value
if (name === "") {
alert("Please enter a valid name");
return;
}

let hubRoute = hubTypeDropdown.value || "default";
let protocol = protocolDropdown.value === "msgpack" ?
new signalR.protocols.msgpack.MessagePackHubProtocol() :
Expand All @@ -148,6 +157,7 @@ <h4>Group Actions</h4>
options.transport = signalR.HttpTransportType[transportDropdown.value];
}

hubRoute = hubRoute + "?name=" + name;
console.log('http://' + document.location.host + '/' + hubRoute);

var connectionBuilder = new signalR.HubConnectionBuilder()
Expand Down Expand Up @@ -205,45 +215,45 @@ <h4>Group Actions</h4>

click('broadcast', function (event) {
let data = getText('message-text');
invoke(connection, 'Send', data);
invoke(connection, 'Send', name, data);
});

click('join-group', function (event) {
let groupName = getText('group-name');
invoke(connection, 'JoinGroup', groupName);
invoke(connection, 'JoinGroup', groupName, name);
});

click('leave-group', function (event) {
let groupName = getText('group-name');
invoke(connection, 'LeaveGroup', groupName);
invoke(connection, 'LeaveGroup', groupName, name);
});

click('groupmsg', function (event) {
let groupName = getText('group-name');
let message = getText('group-message-text');
invoke(connection, 'SendToGroup', groupName, message);
invoke(connection, 'SendToGroup', groupName, name, message);
});

click('others-groupmsg', function (event) {
let groupName = getText('group-name');
let message = getText('group-message-text');
invoke(connection, 'SendToOthersInGroup', groupName, message);
invoke(connection, 'SendToOthersInGroup', groupName, name, message);
});

click('send', function (event) {
let data = getText('me-message-text');
invoke(connection, 'Echo', data);
invoke(connection, 'Echo', name, data);
});

click('broadcast-exceptme', function (event) {
let data = getText('message-text');
invoke(connection, 'SendToOthers', data);
invoke(connection, 'SendToOthers', name, data);
});

click('connection-send', function (event) {
let data = getText('connection-message-text');
let id = getText('connection-id');
invoke(connection, 'SendToConnection', id, data);
invoke(connection, 'SendToConnection', id, name, data);
});

</script>