我怎样才能将消息发送到与signalR特定用户 [英] How Can I Send Message To specific User with signalR

查看:3558
本文介绍了我怎样才能将消息发送到与signalR特定用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,signalR,我不能将消息发送到特定的用户从集线器。

I have some problem with signalR, I can not send message to specific User From Hub.

我想要做的是这样的:

public void Send(string userToId, string userForId, string message)
{

    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHubs>();

    //userForId - it is Session["UserId"]
    context.Clients.User(userForId).updateMessages(message);
}

我已经阅读本主题: http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections ,但目前尚不清楚对我来说,因为我已经不是这个变数名称= Context.User.Identity.Name; 我在会话变量用户信息和第二,当我得到的ConnectionId是这样的: VAR myClientId = $■连接。 hub.id; 的ConnectionId -is改变当我刷新页面或当我点击我的项目的另一个菜单

I have already read this topic: http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections , but It's not clear for me, because I have not this var name = Context.User.Identity.Name; I have User information in Session Variable and second when I get connectionId like this: var myClientId = $.connection.hub.id; connectionId -is changed when I refresh page or when I click another menu on my project.

我有一些问题:
1)我可以将消息发送到特定的用户没有的ConnectionId(只使用会话[用户ID])?
2)一般情况下,我怎么能轻易让一到一个消息传递与SignalR?

I have some questions: 1) Can I send message to specific user without connectionId (use only session["UserId"]) ? 2) In General, How can I make easily one-to-one messaging with SignalR ?

P.S。我工作的ASP.NET MVC(C#)

推荐答案

您可以发送广播消息给所有的用户,无需连接ID。你只需要一个唯一的ID分配给每个用户并将其作为消息参数。

You can send broadcast message to all Users without connection id. You just need to assign a Unique ID to Every user and Send it as Message parameters.

SignalR给出一个唯一的ID,以每个客户端的连接ID。要么你可以使用连接ID,或者你可以在创建客户端分配唯一的ID客户端,并用它作为连接ID。那取决于你要使用的。

SignalR Gives a Unique ID to Every Client as connection ID. Either you can use that connection Id or you can assign unique ID to client while creating client and use it as Connection ID. thats up to you what you want to Use.

修改

更新你的你的方法在你的集线器类文件.....

Just Update Your Your Method in your Hub Class File.....

     public void Send(string name, string message, string connectionid)
{
    // Call the addNewMessageToPage method to update clients.
    Clients.All.addNewMessageToPage(name, message, connectionid);
}

而在你的客户端可以更新添加code包括SignalR文件后: -

And In Your Client Side You can Update Add Code After including SignalR Files:-

        var chat = $.connection.chatHub;


             chat.client.addNewMessageToPage = function (name, message, connectionid) {
            if (connectionid == $('#connection').val()) {

               //   Do What You want Here...
            };
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        $('#connection').val(prompt('Enter your ID:', ''));

        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub.
                chat.server.send($('#displayname').val(), $('#message').val(), $('#connection').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });

希望这有助于...

Hope This Helps...

这篇关于我怎样才能将消息发送到与signalR特定用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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