Update SignalR Sample (#13078)

This commit is contained in:
Mikael Mengistu 2019-08-13 15:12:31 -07:00 committed by GitHub
parent aa34cf3212
commit e7a1dc620b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 64 deletions

View File

@ -9,58 +9,60 @@ namespace SignalRSamples.Hubs
{ {
public class Chat : Hub 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"); 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 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); 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}");
} }
} }
} }

View File

@ -9,53 +9,55 @@ namespace SignalRSamples.Hubs
{ {
public class DynamicChat : DynamicHub 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 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); 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}");
} }
} }
} }

View File

@ -9,53 +9,55 @@ namespace SignalRSamples.Hubs
{ {
public class HubTChat : Hub<IChatClient> 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 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); 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}");
} }
} }

View File

@ -27,6 +27,7 @@
<option value="hubT">Hub&lt;T&gt;</option> <option value="hubT">Hub&lt;T&gt;</option>
</select> </select>
<input type="text" id="displayname" placeholder="Enter User Name" />
<input type="button" id="connect" value="Connect" /> <input type="button" id="connect" value="Connect" />
<input type="button" id="disconnect" value="Disconnect" /> <input type="button" id="disconnect" value="Disconnect" />
@ -58,6 +59,7 @@
<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" />
<input type="hidden" id="displayname" />
</div> </div>
</form> </form>
@ -138,6 +140,13 @@
var connection; var connection;
click('connect', function (event) { click('connect', function (event) {
name = document.getElementById("displayname").value
if (name === "") {
alert("Please enter a valid name");
return;
}
let hubRoute = hubTypeDropdown.value || "default"; let hubRoute = hubTypeDropdown.value || "default";
let protocol = protocolDropdown.value === "msgpack" ? let protocol = protocolDropdown.value === "msgpack" ?
new signalR.protocols.msgpack.MessagePackHubProtocol() : new signalR.protocols.msgpack.MessagePackHubProtocol() :
@ -148,6 +157,7 @@
options.transport = signalR.HttpTransportType[transportDropdown.value]; options.transport = signalR.HttpTransportType[transportDropdown.value];
} }
hubRoute = hubRoute + "?name=" + name;
console.log('http://' + document.location.host + '/' + hubRoute); console.log('http://' + document.location.host + '/' + hubRoute);
var connectionBuilder = new signalR.HubConnectionBuilder() var connectionBuilder = new signalR.HubConnectionBuilder()
@ -205,45 +215,45 @@
click('broadcast', function (event) { click('broadcast', function (event) {
let data = getText('message-text'); let data = getText('message-text');
invoke(connection, 'Send', data); invoke(connection, 'Send', name, data);
}); });
click('join-group', function (event) { click('join-group', function (event) {
let groupName = getText('group-name'); let groupName = getText('group-name');
invoke(connection, 'JoinGroup', groupName); invoke(connection, 'JoinGroup', groupName, name);
}); });
click('leave-group', function (event) { click('leave-group', function (event) {
let groupName = getText('group-name'); let groupName = getText('group-name');
invoke(connection, 'LeaveGroup', groupName); invoke(connection, 'LeaveGroup', groupName, name);
}); });
click('groupmsg', function (event) { click('groupmsg', function (event) {
let groupName = getText('group-name'); let groupName = getText('group-name');
let message = getText('group-message-text'); let message = getText('group-message-text');
invoke(connection, 'SendToGroup', groupName, message); invoke(connection, 'SendToGroup', groupName, name, message);
}); });
click('others-groupmsg', function (event) { click('others-groupmsg', function (event) {
let groupName = getText('group-name'); let groupName = getText('group-name');
let message = getText('group-message-text'); let message = getText('group-message-text');
invoke(connection, 'SendToOthersInGroup', groupName, message); invoke(connection, 'SendToOthersInGroup', groupName, name, 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', name, data);
}); });
click('broadcast-exceptme', function (event) { click('broadcast-exceptme', function (event) {
let data = getText('message-text'); let data = getText('message-text');
invoke(connection, 'SendToOthers', data); invoke(connection, 'SendToOthers', name, data);
}); });
click('connection-send', function (event) { click('connection-send', function (event) {
let data = getText('connection-message-text'); let data = getText('connection-message-text');
let id = getText('connection-id'); let id = getText('connection-id');
invoke(connection, 'SendToConnection', id, data); invoke(connection, 'SendToConnection', id, name, data);
}); });
</script> </script>