信息发送到特定的客户端以及邮件发送的用户 [英] Send message to specific client as well as the message sent user

查看:121
本文介绍了信息发送到特定的客户端以及邮件发送的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SignalR初学者。我创建了一个基于SignalR一个基本的聊天应用程序。我现在面临的问题是,我想发信息给特定的客户端,以及是谁发送消息的用户。如何做到这一点?

我知道这将消息发送到特定的客户端,我们可以做到这一点:

  Clients.Client(Context.ConnectionId).addMessage(数据);

这仅发送消息到指定的客户端不是谁发来的消息之一。

我甚至可以在追加使用jQuery给人一种错误观念,认为该消息已被发送到指定的用户的用户本身的信息。这我不想做的事。

PS:我可以使用()完成在这里?如果是,请解释一下吗?

修改

.js文件

  txtMsg.key preSS(函数(五){
   chat.server.send(chatUsername,selectedUsername,txtMsg.val(),showChatName,_invokeChat);
});chat.client.addMessage =功能(chatUsername,消息,showChatName,invokeChat){
    如果(invokeChat){
        selectedUsername = chatUsername;
        _invokeChat = FALSE;
        chatTitleName.text(聊天着:+ selectedUsername);
    }
    如果(showChatName){
        //要显示的用户名与类型化的消息
        showMessage.append('< B>'+ chatUsername +'< / B>'+':放大器; NBSP;+信息+'< / BR>');
    }
    其他{
        //要仅显示输入信息,而不是用户名
        showMessage.append('和; NBSP;&安培; NBSP;+信息+'< / BR>');
    }
    previousUsername = chatUsername;
    //为了使滚动总是底
    showMessage.scrollTop(showMessage [0] .scrollHeight);
};


.cs文件通过看萨米尔哈菲兹答案更改的)

 公共无效发送(从字符串,字符串,字符串消息,布尔blnShowChatName,布尔blnInvokeChat)
    {
        字符串的ConnectionId = selectUsername(地);
        //调用所有客户端的方法addMessage方法
        Clients.Client(的ConnectionId).addMessage(从日消息,blnShowChatName,blnInvokeChat);
        如果(!from.Equals(到))
        {
            blnInvokeChat = FALSE;
            Clients.Caller.addMessage(从日消息,blnShowChatName,blnInvokeChat);
        }
    }


解决方案

我不会理会发送文本回服务器。你可以只使用JavaScript来追加信息

客户端

  txtMsg.key preSS(函数(五){
   chat.server.send(chatUsername,selectedUsername,txtMsg.val(),showChatName,_invokeChat);   //添加本地此消息,所以我们不必往返于服务器
   方法addMessage(chatUsername,txtMsg.val(),showChatName,_invokeChat);
});chat.client.addMessage =方法addMessage;函数方法addMessage(chatUsername,消息,showChatName,invokeChat){
    如果(invokeChat){
        selectedUsername = chatUsername;
        _invokeChat = FALSE;
        chatTitleName.text(聊天着:+ selectedUsername);
    }
    如果(showChatName){
        //要显示的用户名与类型化的消息
        showMessage.append('< B>'+ chatUsername +'< / B>'+':放大器; NBSP;+信息+'< / BR>');
    }
    其他{
        //要仅显示输入信息,而不是用户名
        showMessage.append('和; NBSP;&安培; NBSP;+信息+'< / BR>');
    }
        previousUsername = chatUsername;
    //为了使滚动总是底
    showMessage.scrollTop(showMessage [0] .scrollHeight);
};

服务器

 公共无效发送(从字符串,字符串,字符串消息,布尔blnShowChatName,布尔blnInvokeChat)
    {
        字符串的ConnectionId = selectUsername(地);
        //调用所有客户端的方法addMessage方法
        Clients.Client(的ConnectionId).addMessage(从日消息,blnShowChatName,blnInvokeChat);
}

这简化了您的服务器code,以及...

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);
        }
    }

解决方案

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...

这篇关于信息发送到特定的客户端以及邮件发送的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆