English 中文(简体)
Send server message to connected clients with Signalr/PersistentConnection
原标题:
  • 时间:2011-09-30 23:16:36
  •  标签:
  • signalr

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?

最佳回答

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());
    });
});
问题回答

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

}
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





相关问题
Enabling push notification with React.js and signalR

I need to enable pushnotifications using signalR import { HubConnectionBuilder} from @microsoft/signalr ; import jwt_decode from "jwt-decode"; let connection = null; ...

SignalR - how to detect disconnected clients

How to detect which clients was disconnected? I m using Hub and saw the chat example where the IDisconnect interface was implemented. De Disconnect() function is called when a page is refreshed or ...

信号呼唤 系统中其他地方的中枢客户

我建立了信号监测中心,供服务器和客户之间沟通。 中心服务器侧代码储存在一个称为Hooking.c的类别中。 我想要的是,能够采用Hooking所定义的方法。

Redirecting a user from within a SignalR Hub class

I have a button that users can click to bid on something. Each bid, broadcasts the latest bid to every other client. That s the reason why I m using SignalR. Now, the user needs to have active ...

热门标签