从 SignalR 调用特定客户端

发布于 2024-12-11 10:03:58 字数 254 浏览 0 评论 0原文

我想从服务器调用特定客户端,而不是广播给所有客户端。问题是我在一些 AJAX 请求的范围内(比如说在 .aspx 代码隐藏中),而不是在 Hub 或 PersistentConnection 中,所以没有 Clients 属性 - 并且进行 ajax (jquery) 调用的客户端不是客户端我想发送信号器消息!

现在,我有一个在 JS 页面加载时调用的集线器,它将新客户端注册到服务器静态列表中,所以我有客户端 Guid。但不知道如何使用它将消息从服​​务器发送到特定客户端。

I want to call specific client from server, and not broadcast to all of them. Problem is that I'm in scope of some AJAX request (in .aspx codebehind let say), and not in Hub or PersistentConnection, so don't have Clients property - and client who made that ajax (jquery) call is not the client I want to send signalr message!

Now, I have one hub that it's called on JS page load, which registers new client into server static list, so I have client Guids. But don't know how to use that to send message from server to specific client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我做我的改变 2024-12-18 10:03:58
$('#sendmessage').click(function () {
    // Call the Send method on the hub. 
    chat.server.send($('#displayname').val(), $('#message').val(), $.connection.hub.id);
    // Clear text box and reset focus for next comment. 
    $('#message').val('').focus();
});

在服务器端发送客户端的 id 并响应该 id

  public void Send ( string name , string message , string connID )
  {
        Clients.Client(connID).broadcastMessage(name , message);
  }
$('#sendmessage').click(function () {
    // Call the Send method on the hub. 
    chat.server.send($('#displayname').val(), $('#message').val(), $.connection.hub.id);
    // Clear text box and reset focus for next comment. 
    $('#message').val('').focus();
});

at server side send the id of the client and response to that id

  public void Send ( string name , string message , string connID )
  {
        Clients.Client(connID).broadcastMessage(name , message);
  }
微暖i 2024-12-18 10:03:58

每次您向中心服务器发送请求时,您的请求都会有不同的连接 ID,因此,我添加了一个包含用户名的静态哈希表 -
不会连续变化,并且信号 r 的连接 id,每次连接时,连接 id 都会更新

 $.connection.hub.start().done(function () {
   chat.server.registerConId($('#displayname').val());
 });

,并在服务器代码中:

public class ChatHub : Hub
{
    private static Hashtable htUsers_ConIds = new Hashtable(20);
    public void registerConId(string userID)
    {
        if(htUsers_ConIds.ContainsKey(userID))
            htUsers_ConIds[userID] = Context.ConnectionId;
        else
            htUsers_ConIds.Add(userID, Context.ConnectionId);
    }
}

Every time you send a request to the hub server, your request will have a different connection id, so, I added a static hash table that contains a username- which
is not changing continuously, and a connection id fro the signal r,every time you connect, the connection id will be updated

 $.connection.hub.start().done(function () {
   chat.server.registerConId($('#displayname').val());
 });

and in the server code:

public class ChatHub : Hub
{
    private static Hashtable htUsers_ConIds = new Hashtable(20);
    public void registerConId(string userID)
    {
        if(htUsers_ConIds.ContainsKey(userID))
            htUsers_ConIds[userID] = Context.ConnectionId;
        else
            htUsers_ConIds.Add(userID, Context.ConnectionId);
    }
}
静谧幽蓝 2024-12-18 10:03:58

当你想向特定id发送消息时

 Clients.Client(Context.ConnectionId).onMessage(
               new Message{From = e.Message.From, Body = e.Message.Body}
               );

when you want to send a message to specific id

 Clients.Client(Context.ConnectionId).onMessage(
               new Message{From = e.Message.From, Body = e.Message.Body}
               );
秋凉 2024-12-18 10:03:58

如果特定用户实际上是调用者本身,您可以使用:

Clients.Caller.myJavaScriptClientFunction();

If the specific user actually is the caller it self, you can use:

Clients.Caller.myJavaScriptClientFunction();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文