How can I send the messages to specific user using user's id which stored in Mssql database?

荒凉一梦 提交于 2021-01-29 11:13:34

问题


I want to implement private chat system with Mssql Database.I want to one to one chat system. One user enter the receiver id which stored in database and message text, then send the message to Receiver . Then the message appears in receiver message area which has that receiver id. In my code works like this; Signalr produce connection id but this id changes every refresh, I want to constant id. How can I do This?

This is my js code

"use strict";


$(document).ready(() => {
    var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build();
    connection.on("connected", connecitonid => $("#connectionId").html(connecitonid));

    connection.start();

    $("button").click(() => {
        let message = $("#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 = "me" + ":" + message;
        document.getElementById("son").appendChild(div);
    });

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

});

This is my hub class

using MentorShip.Models;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks;

namespace MentorShip.Hubs
{
    public class SignalRChat:Hub
    {
        

        public async Task ClientSendMessage(string connectionId,string user, string message)
        {
            await Clients.Client(connectionId).SendAsync("ReceiveMessage",user, message);
        }
        public async override Task OnConnectedAsync()
        {
            await Clients.Caller.SendAsync("connected", Context.ConnectionId);
          
        }

    }
}

this is my html code

 <div class="container">
            <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="Sender Name"></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"> <input type="text" id="txtMessage"> <button>Send</button></div>
            </div>
            <div class="row">
                <div class="col-md-7" id="son"> </div>
            </div>


        </div>
        

this is how codes work This connection id changes every refresh or another browser not constant

来源:https://stackoverflow.com/questions/65581679/how-can-i-send-the-messages-to-specific-user-using-users-id-which-stored-in-mss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!