使用 Signalr/PersistentConnection 将服务器消息发送到连接的客户端

发布于 2024-12-07 09:14:35 字数 159 浏览 1 评论 0原文

我正在使用 SignalR/PersistentConnection,而不是集线器。

我想从服务器向客户端发送消息。我有客户端 ID 来发送它,但是如何从服务器向客户端发送消息?

就像,当服务器上发生某些事件时,我们希望向特定用户发送通知。

有什么想法吗?

I´m using SignalR/PersistentConnection, not the Hub.

I want to send a message from the server to client. I have the client id to send it, but how can I send a message from server to the client?

Like, when some event happens on server, we want send a notification to a particular user.

Any ideas?

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

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

发布评论

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

评论(3

诗酒趁年少 2024-12-14 09:14:35

github 页面展示了如何使用 PersistentConnections 来执行此操作。

public class MyConnection : PersistentConnection {
    protected override Task OnReceivedAsync(string clientId, string data) {
        // Broadcast data to all clients
        return Connection.Broadcast(data);
    }
}

Global.asax

using System;
using System.Web.Routing;
using SignalR.Routing;

public class Global : System.Web.HttpApplication {
    protected void Application_Start(object sender, EventArgs e) {
        // Register the route for chat
        RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
    }
}

然后在客户端:

$(function () {
    var connection = $.connection('echo');

    connection.received(function (data) {
        $('#messages').append('<li>' + data + '</li>');
    });

    connection.start();

    $("#broadcast").click(function () {
        connection.send($('#msg').val());
    });
});

The github page shows how to do this using PersistentConnections.

public class MyConnection : PersistentConnection {
    protected override Task OnReceivedAsync(string clientId, string data) {
        // Broadcast data to all clients
        return Connection.Broadcast(data);
    }
}

Global.asax

using System;
using System.Web.Routing;
using SignalR.Routing;

public class Global : System.Web.HttpApplication {
    protected void Application_Start(object sender, EventArgs e) {
        // Register the route for chat
        RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
    }
}

Then on the client:

$(function () {
    var connection = $.connection('echo');

    connection.received(function (data) {
        $('#messages').append('<li>' + data + '</li>');
    });

    connection.start();

    $("#broadcast").click(function () {
        connection.send($('#msg').val());
    });
});
不寐倦长更 2024-12-14 09:14:35

可能是帮助中的AddToGroup函数
在服务器端

将客户端放入不同的通道

public bool Join(string channel)
{
    AddToGroup(channel.ToString()).Wait();
    return true;
}

然后他们可以在不同的通道中发送消息

public void Send(string channel, string message)
{

    String id = this.Context.ClientId;

    Clients[channel.ToString()].addMessage(message);

}

May be the AddToGroup function in the helps
in Server side

Put the clients in to different channels

public bool Join(string channel)
{
    AddToGroup(channel.ToString()).Wait();
    return true;
}

Then they Can send out message in different channel

public void Send(string channel, string message)
{

    String id = this.Context.ClientId;

    Clients[channel.ToString()].addMessage(message);

}
薄荷港 2024-12-14 09:14:35
using SignalR;
using SignalR.Hosting.AspNet;
using SignalR.Infrastructure;

public class MyConnection : PersistentConnection
{
}

public class Notifier
{
  public void Notify(string clientId, object data) {
    MyConnection connection = (MyConnection) AspNetHost.DependencyResolver
      .Resolve<IConnectionManager()
      .GetConnection<MyConnection>();

    connection.Send(clientId, data);
  }
}

using SignalR;
using SignalR.Hosting.AspNet;
using SignalR.Infrastructure;

public class MyConnection : PersistentConnection
{
}

public class Notifier
{
  public void Notify(string clientId, object data) {
    MyConnection connection = (MyConnection) AspNetHost.DependencyResolver
      .Resolve<IConnectionManager()
      .GetConnection<MyConnection>();

    connection.Send(clientId, data);
  }
}

https://github.com/SignalR/SignalR/wiki/PersistentConnection

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