gpt4 book ai didi

javascript - 如何使用组向信号器中的特定用户发送消息?

转载 作者:行者123 更新时间:2023-11-28 03:04:39 24 4
gpt4 key购买 nike

我是 ASP.NET Core 新手,我正在尝试使用 signalR 开发聊天应用程序。我正在使用asp.net core mvc,根据我之前的研究,我已经成功地实现了聊天,但问题是他们都可以接收消息,我确实尝试使用client.group,它是否有效,我现在可以发送向特定组发送消息。我想使用 client.group 而不是 client.user 向特定人员发送和接收消息,这可能吗?我的中心里有这个代码。

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

public async Task AddToGroup(string groupName)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has joined the group {groupName}.");
}

public async Task RemoveFromGroup(string groupName)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);

await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has left the group {groupName}.");
}

在我的客户端

const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.build();
connection.on("Send", function (message) {
var li = document.createElement("li");
li.textContent = message;
document.getElementById("messagesList").appendChild(li);
});
document.getElementById("groupmsg").addEventListener("click", async (event) => {
var groupName = document.getElementById("group-name").value;
var groupMsg = document.getElementById("group-message-text").value;
try {
await connection.invoke("SendMessageToGroup", groupName, groupMsg);
}
catch (e) {
console.error(e.toString());
}
event.preventDefault();
});
document.getElementById("join-group").addEventListener("click", async (event) => {
var groupName = document.getElementById("group-name").value;
try {
await connection.invoke("AddToGroup", groupName);
}
catch (e) {
console.error(e.toString());
}
event.preventDefault();
});
document.getElementById("leave-group").addEventListener("click", async (event) => {
var groupName = document.getElementById("group-name").value;
try {
await connection.invoke("RemoveFromGroup", groupName);
}
catch (e) {
console.error(e.toString());
}
event.preventDefault();
});
// We need an async function in order to use await, but we want this code to run immediately,
// so we use an "immediately-executed async function"
(async () => {
try {
await connection.start();
}
catch (e) {
console.error(e.toString());
}
})();

最后这是我的观点

<div class="container">
<div class="row">&nbsp;</div>
<div class="row">
<div class="col-6">&nbsp;</div>
<div class="col-6">
<form class="form-inline">
<div class="input-prepend input-append">
<input type="text" name="group-message" id="group-message-text" placeholder="Type a message" />
<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="join-group" class="btn" value="Join Group" />
<input type="button" id="leave-group" class="btn" value="Leave Group" />
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-12">
<hr />
</div>
</div>
<div class="row">
<div class="col-6">&nbsp;</div>
<div class="col-6">
<ul id="messagesList"></ul>
</div>
</div>
</div>
<script src="~/js/signalr.js"></script>
<script src="~/js/chat.js"></script>

最佳答案

I want to send and receive message from specific person using client.group instead of client.user, is it possible?

是的,您可以通过使用单用户组来实现。

1)为每个用户创建一个组,并以用户名命名该组

2) 然后,当您只想联系该用户时,向该群组发送消息,如下所示。

Clients.Group(who).SendAsync("Send", $"{Context.ConnectionId}: {message}")

关于javascript - 如何使用组向信号器中的特定用户发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60715278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com