Send message to specific client as well as the mes

2019-07-23 20:25发布

问题:

I am beginner in SignalR. I created a basic chat application based on SignalR. The problem I am facing is that I want to send message to the specific client as well as the user who sent the message. How to do this?

I know that to send a message to a specific client we can do this:

Clients.Client(Context.ConnectionId).addMessage(data);

which only sends message to the specified client not the one who sent the message.

I can even append the message at the user itself using jQuery giving a false belief that the message has been sent to the specified user. which I dont want to do.

PS: Can I use done() here? If yes, please explain?

EDIT:

.js file

txtMsg.keypress(function (e) {
   chat.server.send(chatUsername, selectedUsername, txtMsg.val(), showChatName, _invokeChat);
});

chat.client.addMessage = function (chatUsername, message, showChatName, invokeChat) {
    if (invokeChat) {
        selectedUsername = chatUsername;
        _invokeChat = false;
        chatTitleName.text("Chat with: " + selectedUsername);
    }
    if (showChatName) {
        //To show Username with the typed message
        showMessage.append('<b>' + chatUsername + '</b>' + ':&nbsp;' + message + '</br>');
    }
    else {
        //To show only typed message, not the username
        showMessage.append('&nbsp;&nbsp;' + message + '</br>');
    }
    previousUsername = chatUsername;
    //To keep scroll always bottom
    showMessage.scrollTop(showMessage[0].scrollHeight);
};

.cs file (Changed by seeing Samir hafez answer)

public void Send(string from, string to, string message, bool blnShowChatName, bool blnInvokeChat)
    {
        string ConnectionID = selectUsername(to);
        // Call the addMessage method on all clients    
        Clients.Client(ConnectionID).addMessage(from, message, blnShowChatName, blnInvokeChat);
        if (!from.Equals(to))
        {
            blnInvokeChat = false;
            Clients.Caller.addMessage(from, message, blnShowChatName, blnInvokeChat);
        }
    }

回答1:

I wouldn't bother sending the text back to the server. You could just use javascript to append the message

client

txtMsg.keypress(function (e) {
   chat.server.send(chatUsername, selectedUsername, txtMsg.val(), showChatName, _invokeChat);

   //add this message locally so we don't have to round trip to the server
   addMessage(chatUsername, txtMsg.val(), showChatName, _invokeChat);
});

chat.client.addMessage = addMessage;

function addMessage(chatUsername, message, showChatName, invokeChat) {
    if (invokeChat) {
        selectedUsername = chatUsername;
        _invokeChat = false;
        chatTitleName.text("Chat with: " + selectedUsername);
    }
    if (showChatName) {
        //To show Username with the typed message
        showMessage.append('<b>' + chatUsername + '</b>' + ':&nbsp;' + message + '</br>');
    }
    else {
        //To show only typed message, not the username
        showMessage.append('&nbsp;&nbsp;' + message + '</br>');
    }
        previousUsername = chatUsername;
    //To keep scroll always bottom
    showMessage.scrollTop(showMessage[0].scrollHeight);
};

server

public void Send(string from, string to, string message, bool blnShowChatName, bool blnInvokeChat)
    {
        string ConnectionID = selectUsername(to);
        // Call the addMessage method on all clients    
        Clients.Client(ConnectionID).addMessage(from, message, blnShowChatName, blnInvokeChat);
}

This simplifies your server code as well...



回答2:

Clients.Caller will give you the calling client.

Or the more verbose way: Clients.Client(Context.ConnectionId)

All this is available to you at the SignalR Wiki for Hubs Here