繁体   English   中英

如何使用存储在数据库中的用户 email 地址将消息发送给特定用户?

[英]How can I send the message to a specific user using the user's email address stored in db?

我在 ASP.NET Core 2.1 MVC SignalR 有一个项目。 当我开始这个项目时,我没有授权 select 个人帐户选项。 我想使用存储在数据库中的用户的 email 地址而不是连接 ID 发送消息,但我无法实现这一点。 我该如何解决这个问题?

这是我的集线器 class 代码:

 public class SignalRChat:Hub
    {
        
        Context c = new Context();
        public async Task setUserEmail(string email)
        {
            string id = Context.ConnectionId;
            c.EmailConnectionIds.Where(a => a.connection_id == id).FirstOrDefault().email = email;
        }
        
        public async Task ClientSendMessage(string connectionId,string user, string message)
        {
           var deger= c.EmailConnectionIds.Where(a => a.connection_id ==
            connectionId).FirstOrDefault();
            

            await Clients.Client(deger.connection_id).SendAsync("ReceiveMessage",user, message);
        }


        public override async Task OnConnectedAsync()
        {

            EmailConnectionId val = new EmailConnectionId();
            val.connection_id = Context.ConnectionId; ;
            val.email = "";
           
            c.EmailConnectionIds.Add(val);
            c.SaveChanges();

           await  base.OnConnectedAsync();


        }
        public override Task OnDisconnectedAsync(Exception exception)
        {
            var connection = c.EmailConnectionIds.Where(a => a.connection_id ==
            Context.ConnectionId).FirstOrDefault();

            if (connection != null)
            {
                c.EmailConnectionIds.Remove(connection);
            }

            return base.OnDisconnectedAsync(exception);
        }

    }
    

这是我的代码:

"use strict";

$(document).ready(() => {
    var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();
  

     var email = $("#client").val();
  
    connection.start().then(() => connection.invoke('setUserEmail ', email));

    $("#msg-send").click(() => {
        let message = $("#txtMessage").val();
        $("#txtMessage").val(" ");
        var user = $("#sender").val();
        connection.invoke("ClientSendMessage", $("#client").val(), user, message)
            .catch(error => console.log("Error." + error));       
        var div = document.createElement("div");
        div.textContent = message;
        
        document.getElementById("chat-cont").appendChild(div);
    });

    connection.on("ReceiveMessage", function (user, message) {
        var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        var encodedMsg =  msg;
        var div = document.createElement("div");
        div.textContent = encodedMsg;
        
        document.getElementById("chat-cont").appendChild(div);
    });
});

HTML 代码:

<div class="container">
    <div id="chat-cont" class="clearfix" style="max-height:450px;overflow:scroll;">
    </div>

    <div style="margin-top:470px;margin-left:40%;bottom:50px !important;" class="footer navbar-fixed-bottom">
        <div class="row">
            <h5>Connection ID : <span id="connectionId"></span></h5>
        </div>
        <div class="row">
            <div class="col-md-7"><input type="text" id="sender" value="@ViewBag.message"></div>
        </div>
        <div class="row">
            <div class="col-md-7"><input type="text" placeholder="ReceiverId" id="client"></div>
        </div>
        <div class="row">
            <div class="col-md-7" style="position:relative;"> <input type="text" id="txtMessage" class="form-control" style="width:70%;"></div>
            <div class="col-md-5" style="position:absolute;margin-left:40%;"> <button id="msg-send" class="btn btn-success">Send</button></div>
        </div>
    </div>
</div>

我建议您创建一个当前连接列表CurrentConnections

static HashSet<EmailConnectionID> CurrentConnections = new HashSet<EmailConnectionID>();
class EmailConnectionID
{
    public string email { get; set; }
    public string connection_id { get; set; }
}

将条目添加到 CurrentConnections OnConnectedAsync

public override async Task<Task> OnConnectedAsync()
{

EmailConnectionID val = new EmailConnectionID();
val.connection_id = Context.ConnectionId; ;
val.email = "";
CurrentConnections.Add(val);

return base.OnConnectedAsync();
}

然后在客户端连接后,您要做的第一件事是将 email 发送到hub并使用 email 编辑 CurrentConnections 列表中的条目

public async Task setUserEmail(string email)
{
string id = Context.ConnectionId;
CurrentConnections.Where(a => a.connection_id == id).FirstOrDefault().email = email;
}

您应该从 CurrentConnections 列表OnDisconnectedAsync中删除实体

public override Task OnDisconnectedAsync(Exception exception)
{
var connection = CurrentConnections.Where(a => a.connection_id == 
Context.ConnectionId).FirstOrDefault();

if (connection != null)
{
    CurrentConnections.Remove(connection);
}

return base.OnDisconnectedAsync(exception);
}

之后您可以根据 email 地址发送

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM