Skip to content

Commit e7a1dc6

Browse files
authored
Update SignalR Sample (#13078)
1 parent aa34cf3 commit e7a1dc6

File tree

4 files changed

+80
-64
lines changed

4 files changed

+80
-64
lines changed

src/SignalR/samples/SignalRSamples/Hubs/Chat.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,60 @@ namespace SignalRSamples.Hubs
99
{
1010
public class Chat : Hub
1111
{
12-
public override async Task OnConnectedAsync()
12+
public override Task OnConnectedAsync()
1313
{
14-
await Clients.All.SendAsync("Send", $"{Context.ConnectionId} joined");
14+
var name = Context.GetHttpContext().Request.Query["name"];
15+
return Clients.All.SendAsync("Send", $"{name} joined the chat");
1516
}
1617

17-
public override async Task OnDisconnectedAsync(Exception ex)
18+
public override Task OnDisconnectedAsync(Exception exception)
1819
{
19-
await Clients.Others.SendAsync("Send", $"{Context.ConnectionId} left");
20+
var name = Context.GetHttpContext().Request.Query["name"];
21+
return Clients.All.SendAsync("Send", $"{name} left the chat");
2022
}
2123

22-
public Task Send(string message)
24+
public Task Send(string name, string message)
2325
{
24-
return Clients.All.SendAsync("Send", $"{Context.ConnectionId}: {message}");
26+
return Clients.All.SendAsync("Send", $"{name}: {message}");
2527
}
2628

27-
public Task SendToOthers(string message)
29+
public Task SendToOthers(string name, string message)
2830
{
29-
return Clients.Others.SendAsync("Send", $"{Context.ConnectionId}: {message}");
31+
return Clients.Others.SendAsync("Send", $"{name}: {message}");
3032
}
3133

32-
public Task SendToConnection(string connectionId, string message)
34+
public Task SendToConnection(string connectionId, string name, string message)
3335
{
34-
return Clients.Client(connectionId).SendAsync("Send", $"Private message from {Context.ConnectionId}: {message}");
36+
return Clients.Client(connectionId).SendAsync("Send", $"Private message from {name}: {message}");
3537
}
3638

37-
public Task SendToGroup(string groupName, string message)
39+
public Task SendToGroup(string groupName, string name ,string message)
3840
{
39-
return Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
41+
return Clients.Group(groupName).SendAsync("Send", $"{name}@{groupName}: {message}");
4042
}
4143

42-
public Task SendToOthersInGroup(string groupName, string message)
44+
public Task SendToOthersInGroup(string groupName, string name,string message)
4345
{
44-
return Clients.OthersInGroup(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
46+
return Clients.OthersInGroup(groupName).SendAsync("Send", $"{name}@{groupName}: {message}");
4547
}
4648

47-
public async Task JoinGroup(string groupName)
49+
public async Task JoinGroup(string groupName, string name)
4850
{
4951
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
5052

51-
await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} joined {groupName}");
53+
await Clients.Group(groupName).SendAsync("Send", $"{name} joined {groupName}");
5254
}
5355

54-
public async Task LeaveGroup(string groupName)
56+
public async Task LeaveGroup(string groupName, string name)
5557
{
56-
await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} left {groupName}");
58+
await Clients.Group(groupName).SendAsync("Send", $"{name} left {groupName}");
5759

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

61-
public Task Echo(string message)
63+
public Task Echo(string name, string message)
6264
{
63-
return Clients.Caller.SendAsync("Send", $"{Context.ConnectionId}: {message}");
65+
return Clients.Caller.SendAsync("Send", $"{name}: {message}");
6466
}
6567
}
6668
}

src/SignalR/samples/SignalRSamples/Hubs/DynamicChat.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,55 @@ namespace SignalRSamples.Hubs
99
{
1010
public class DynamicChat : DynamicHub
1111
{
12-
public override async Task OnConnectedAsync()
12+
public override Task OnConnectedAsync()
1313
{
14-
await Clients.All.Send($"{Context.ConnectionId} joined");
14+
var name = Context.GetHttpContext().Request.Query["name"];
15+
return Clients.All.Send($"{name} joined the chat");
1516
}
1617

17-
public override async Task OnDisconnectedAsync(Exception ex)
18+
public override Task OnDisconnectedAsync(Exception exception)
1819
{
19-
await Clients.Others.Send($"{Context.ConnectionId} left");
20+
var name = Context.GetHttpContext().Request.Query["name"];
21+
return Clients.All.Send($"{name} left the chat");
2022
}
2123

22-
public Task Send(string message)
24+
public Task Send(string name, string message)
2325
{
24-
return Clients.All.Send($"{Context.ConnectionId}: {message}");
26+
return Clients.All.Send($"{name}: {message}");
2527
}
2628

27-
public Task SendToOthers(string message)
29+
public Task SendToOthers(string name, string message)
2830
{
29-
return Clients.Others.Send($"{Context.ConnectionId}: {message}");
31+
return Clients.Others.Send($"{name}: {message}");
3032
}
3133

32-
public Task SendToGroup(string groupName, string message)
34+
public Task SendToGroup(string groupName, string name, string message)
3335
{
34-
return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
36+
return Clients.Group(groupName).Send($"{name}@{groupName}: {message}");
3537
}
3638

37-
public Task SendToOthersInGroup(string groupName, string message)
39+
public Task SendToOthersInGroup(string groupName, string name, string message)
3840
{
39-
return Clients.OthersInGroup(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
41+
return Clients.OthersInGroup(groupName).Send($"{name}@{groupName}: {message}");
4042
}
4143

42-
public async Task JoinGroup(string groupName)
44+
public async Task JoinGroup(string groupName, string name)
4345
{
4446
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
4547

46-
await Clients.Group(groupName).Send($"{Context.ConnectionId} joined {groupName}");
48+
await Clients.Group(groupName).Send($"{name} joined {groupName}");
4749
}
4850

49-
public async Task LeaveGroup(string groupName)
51+
public async Task LeaveGroup(string groupName, string name)
5052
{
51-
await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}");
53+
await Clients.Group(groupName).Send($"{name} left {groupName}");
5254

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

56-
public Task Echo(string message)
58+
public Task Echo(string name, string message)
5759
{
58-
return Clients.Caller.Send($"{Context.ConnectionId}: {message}");
60+
return Clients.Caller.Send($"{name}: {message}");
5961
}
6062
}
6163
}

src/SignalR/samples/SignalRSamples/Hubs/HubTChat.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,55 @@ namespace SignalRSamples.Hubs
99
{
1010
public class HubTChat : Hub<IChatClient>
1111
{
12-
public override async Task OnConnectedAsync()
12+
public override Task OnConnectedAsync()
1313
{
14-
await Clients.All.Send($"{Context.ConnectionId} joined");
14+
var name = Context.GetHttpContext().Request.Query["name"];
15+
return Clients.All.Send($"{name} joined the chat");
1516
}
1617

17-
public override async Task OnDisconnectedAsync(Exception ex)
18+
public override Task OnDisconnectedAsync(Exception exception)
1819
{
19-
await Clients.Others.Send($"{Context.ConnectionId} left");
20+
var name = Context.GetHttpContext().Request.Query["name"];
21+
return Clients.All.Send($"{name} left the chat");
2022
}
2123

22-
public Task Send(string message)
24+
public Task Send(string name, string message)
2325
{
24-
return Clients.All.Send($"{Context.ConnectionId}: {message}");
26+
return Clients.All.Send($"{name}: {message}");
2527
}
2628

27-
public Task SendToOthers(string message)
29+
public Task SendToOthers(string name, string message)
2830
{
29-
return Clients.Others.Send($"{Context.ConnectionId}: {message}");
31+
return Clients.Others.Send($"{name}: {message}");
3032
}
3133

32-
public Task SendToGroup(string groupName, string message)
34+
public Task SendToGroup(string groupName, string name, string message)
3335
{
34-
return Clients.Group(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
36+
return Clients.Group(groupName).Send($"{name}@{groupName}: {message}");
3537
}
3638

37-
public Task SendToOthersInGroup(string groupName, string message)
39+
public Task SendToOthersInGroup(string groupName, string name, string message)
3840
{
39-
return Clients.OthersInGroup(groupName).Send($"{Context.ConnectionId}@{groupName}: {message}");
41+
return Clients.OthersInGroup(groupName).Send($"{name}@{groupName}: {message}");
4042
}
4143

42-
public async Task JoinGroup(string groupName)
44+
public async Task JoinGroup(string groupName, string name)
4345
{
4446
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
4547

46-
await Clients.Group(groupName).Send($"{Context.ConnectionId} joined {groupName}");
48+
await Clients.Group(groupName).Send($"{name} joined {groupName}");
4749
}
4850

49-
public async Task LeaveGroup(string groupName)
51+
public async Task LeaveGroup(string groupName, string name)
5052
{
51-
await Clients.Group(groupName).Send($"{Context.ConnectionId} left {groupName}");
53+
await Clients.Group(groupName).Send($"{name} left {groupName}");
5254

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

56-
public Task Echo(string message)
58+
public Task Echo(string name, string message)
5759
{
58-
return Clients.Caller.Send($"{Context.ConnectionId}: {message}");
60+
return Clients.Caller.Send($"{name}: {message}");
5961
}
6062
}
6163

src/SignalR/samples/SignalRSamples/wwwroot/hubs.html

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ <h1 id="head1">SignalR Hubs Sample</h1>
2727
<option value="hubT">Hub&lt;T&gt;</option>
2828
</select>
2929

30+
<input type="text" id="displayname" placeholder="Enter User Name" />
3031
<input type="button" id="connect" value="Connect" />
3132
<input type="button" id="disconnect" value="Disconnect" />
3233

@@ -58,6 +59,7 @@ <h4>To Me</h4>
5859
<div class="input-append">
5960
<input type="text" id="me-message-text" placeholder="Type a message" />
6061
<input type="button" id="send" class="btn" value="Send to Me" />
62+
<input type="hidden" id="displayname" />
6163
</div>
6264
</form>
6365

@@ -138,6 +140,13 @@ <h4>Group Actions</h4>
138140
var connection;
139141

140142
click('connect', function (event) {
143+
144+
name = document.getElementById("displayname").value
145+
if (name === "") {
146+
alert("Please enter a valid name");
147+
return;
148+
}
149+
141150
let hubRoute = hubTypeDropdown.value || "default";
142151
let protocol = protocolDropdown.value === "msgpack" ?
143152
new signalR.protocols.msgpack.MessagePackHubProtocol() :
@@ -148,6 +157,7 @@ <h4>Group Actions</h4>
148157
options.transport = signalR.HttpTransportType[transportDropdown.value];
149158
}
150159

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

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

206216
click('broadcast', function (event) {
207217
let data = getText('message-text');
208-
invoke(connection, 'Send', data);
218+
invoke(connection, 'Send', name, data);
209219
});
210220

211221
click('join-group', function (event) {
212222
let groupName = getText('group-name');
213-
invoke(connection, 'JoinGroup', groupName);
223+
invoke(connection, 'JoinGroup', groupName, name);
214224
});
215225

216226
click('leave-group', function (event) {
217227
let groupName = getText('group-name');
218-
invoke(connection, 'LeaveGroup', groupName);
228+
invoke(connection, 'LeaveGroup', groupName, name);
219229
});
220230

221231
click('groupmsg', function (event) {
222232
let groupName = getText('group-name');
223233
let message = getText('group-message-text');
224-
invoke(connection, 'SendToGroup', groupName, message);
234+
invoke(connection, 'SendToGroup', groupName, name, message);
225235
});
226236

227237
click('others-groupmsg', function (event) {
228238
let groupName = getText('group-name');
229239
let message = getText('group-message-text');
230-
invoke(connection, 'SendToOthersInGroup', groupName, message);
240+
invoke(connection, 'SendToOthersInGroup', groupName, name, message);
231241
});
232242

233243
click('send', function (event) {
234244
let data = getText('me-message-text');
235-
invoke(connection, 'Echo', data);
245+
invoke(connection, 'Echo', name, data);
236246
});
237247

238248
click('broadcast-exceptme', function (event) {
239249
let data = getText('message-text');
240-
invoke(connection, 'SendToOthers', data);
250+
invoke(connection, 'SendToOthers', name, data);
241251
});
242252

243253
click('connection-send', function (event) {
244254
let data = getText('connection-message-text');
245255
let id = getText('connection-id');
246-
invoke(connection, 'SendToConnection', id, data);
256+
invoke(connection, 'SendToConnection', id, name, data);
247257
});
248258

249259
</script>

0 commit comments

Comments
 (0)