Save private chat in database using SignalR in ASP.Net MVC

Last Reply one year ago By mukesh1

Posted one year ago

my users are already registered.

I want that if i click on any user then start private chat with that user and also want to save to database and also want to reload older chat with that user.

You are viewing reply posted by: mukesh1 one year ago.
Posted one year ago

 

var chatHub = $.connection.chatHub;
registerClientMethods(chatHub);
$.connection.hub.start().done(function () {
    var usrid = @ViewBag.userid;
    var Touserid=@ViewBag.Touserid;
    chatHub.server.connect(usrid, Touserid);
    loadEvents(objHub);
});

 

       function registerClientMethods(chatHub) {
           chatHub.client.sendPrivateMessage = function (windowId, fromUserName, message, userimg, CurrentDateTime) {
                var ctrId = 'private_' + windowId;
                if ($('#' + ctrId).length == 0) {

                    OpenPrivateChatBox(chatHub, windowId, ctrId, fromUserName, userimg);

                }

                var CurrUser = $('#hdUserName').val();
                var Side = 'right';
                var TimeSide = 'left';

                if (CurrUser == fromUserName) {
                    Side = 'left';
                    TimeSide = 'right';

                }
                else {
                    var Notification = 'New Message From ' + fromUserName;
                    IntervalVal = setInterval("ShowTitleAlert('SignalR Chat App', '" + Notification + "')", 800);

                    var msgcount = $('#' + ctrId).find('#MsgCountP').html();
                    msgcount++;
                    $('#' + ctrId).find('#MsgCountP').html(msgcount);
                    $('#' + ctrId).find('#MsgCountP').attr("title", msgcount + ' New Messages');
                }

                var divChatP = '<div class="direct-chat-msg ' + Side + '">' +
                    '<div class="direct-chat-info clearfix">' +
                    '<span class="direct-chat-name pull-' + Side + '">' + fromUserName + '</span>' +
                    '<span class="direct-chat-timestamp pull-' + TimeSide + '"">' + CurrentDateTime + '</span>' +
                    '</div>' +

                    ' <img class="direct-chat-img" src="' + userimg + '" alt="Message User Image">' +
                    ' <div class="direct-chat-text" >' + message + '</div> </div>';

                $('#' + ctrId).find('#divMessage').append(divChatP);

                // Apply Slim Scroll Bar in Private Chat Box
                var ScrollHeight = $('#' + ctrId).find('#divMessage')[0].scrollHeight;
                $('#' + ctrId).find('#divMessage').slimScroll({
                    height: ScrollHeight
                });
            }

        }

 

        public void Connect(int usrid, int Touserid)
        {

            //var id = TempData.Peek("ID");
            ////var id = 8;
            //int id1 = Convert.ToInt32(id.ToString());
            var connectionid = Context.ConnectionId;
            var connection = db.OnlineUsers.AsNoTracking().Where(o => o.UserID == usrid).FirstOrDefault();
            var connection1 = db.OnlineUsers.AsNoTracking().Where(o => o.UserID == Touserid).FirstOrDefault();
            // send to caller
            Clients.Caller.onConnected(connectionid, usrinfo.First_Name + " " + usrinfo.Last_Name, ConnectedUsers, CurrentMessage);

            // send to all except caller client
            Clients.AllExcept(connectionid).onNewUserConnected(connectionid, usrinfo.First_Name + " " + usrinfo.Last_Name, logintime);

 

        public void SendPrivateMessage(string toUserId, string message)
        {

            string fromUserId = Context.ConnectionId;

            var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionID == toUserId);
            var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionID == fromUserId);

            if (toUser != null && fromUser != null)
            {
                string CurrentDateTime = DateTime.Now.ToString();
               // string UserImg = GetUserImage(fromUser.UserName);
                // send to 
                Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message, CurrentDateTime);

                // send to caller user
                Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message, CurrentDateTime);
            }
        }