Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I searched and tried many way but I couldn't. There are information about group chat on web but there are not about private chat. what way should i follow? Can u share article or advice about this? This is my last stage of my project. Please.

C#
public void Send(string user, string content, string recipientId,string image)
{
    var sender = _userService.GetByUserName(Context.User.Identity.Name);
    Message message = new Message
    {
        Content = content,
        RecipientId = recipientId,
        SenderId = sender.Id
    };
    _db.Message.Add(message);
    _db.SaveChange();
    Clients.Users(recipientId, sender.Id).SendAsync("ReceiveMessage", user, content, image);
}


JavaScript
<pre>"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();



connection.on("ReceiveMessage", function (user, content,image) {
 
    var d = new Date();
    var date = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    var msg = content;
    var kullanici = user;
    var img = new Image();
    img.src =image;

    document.querySelector("#messageList").innerHTML += "<li class='left clearfix'>" +
        "<span class='chat-img pull-left'>"
        + "<img src='"+ img.src + "' width='50' height='50'/>" +
        "</span>" +
        "<div class='chat-body clearfix' style='color: white'>" +
        " <div class='header'>" +
        "" style="color: rgba(0, 0, 255, 1)">" + kullanici + "" + "<small class='pull-right text-muted'>" +
        "<span class='glyphicon glyphicon - time'>" + "</span>" + date +
        " </small >" +
        "</div >" +
        " <p style='color:white'>" +
        msg +
        "</p >" +
        "</div >" +
        " </li >";

});

connection.start().then(function () {
    document.querySelector("#sendButton").disabled = false;
}).catch(function (err) {
    return console.error(err.toString());
});

document.querySelector("#sendButton").addEventListener("click", function (event) {
    var user = document.querySelector("#userInput").value;
    var content = document.querySelector("#messageInput").value;
    var recipientId = document.querySelector("#recipient").value;
    var image = document.querySelector("#userImage1").src;
    
    connection.invoke("Send", user, content, recipientId,image).catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});


What I have tried:

I searched and tried many way but I couldn't.
Posted
Updated 18-May-20 10:23am
v3
Comments
F-ES Sitecore 18-May-20 15:25pm    
The hard part of making an online chat is managing the messages etc. If you have found a "group chat" then all you have to do is modify the code to restrict who can join the chat, especially if the code already covers multiple chats.
Member 14769019 18-May-20 15:51pm    
I want to uplaod pics. Can I?
MadMyche 18-May-20 16:05pm    
Not pics, but you can post the relevant that code you do have
Member 14769019 18-May-20 16:21pm    
But I cant tell another way.
This is step1: https://www.imageupload.net/image/pjrPZ

this is step 2: https://www.imageupload.net/image/pjzVb
Member 14769019 18-May-20 16:21pm    
And ı will sahare my code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900