[en] Invoke Method from server back to client (solved)

Topics: Technical Support
Apr 2, 2013 at 8:16 AM
I am still learning C#. And would like some assistance on how to invoke the action type from server back to clients

Example:
Client > Local Server > Master Server > Remote Local Server > Remote Client

I can get the following to be executed up the chain and back down to the remote local server.
public event Action<string, string> BroadCast;
However how would I envoke the BroadCast action from the Remote Local Server when it receives the BroadCast and trigger the on the clients connected on the remote server.

How could I accomplish this?

Clint.
Coordinator
Apr 3, 2013 at 11:41 AM
Edited Apr 5, 2013 at 2:03 PM
Hi Clint,

Here is a simple example of the master-slave setup:
//----------------------------
// Shared library project
 
public interface IBroadcastService
{
  event Action<string, string> Broadcast;
 
  void BroadcastMessage(string title, string message);
}
 
//----------------------------
// Master server project
 
internal class BroadcastService : IBroadcastService
{
  public event Action<string, string> Broadcast;
 
  public void BroadcastMessage(string title, string message)
  {
    var broadcast = Broadcast;
    if (broadcast != null)
    {
      broadcast(title, message);
    }
  }
}
 
static class MasterServer
{
  public static void Main()
  {
    var host = new ZyanComponentHost("Master", 8765);
    host.RegisterComponent<IBroadcastService, BroadcastService>();
 
    Console.WriteLine("Master server started. Press ENTER to quit.");
    Console.ReadLine();
  }
}
 
//----------------------------
// Slave server project
 
internal class RebroadcastService : IBroadcastService
{
  public RebroadcastService(IBroadcastService masterServer)
  {
    MasterServer = masterServer;
    MasterServer.Broadcast += (title, message) =>
      BroadcastMessageToLocalSubscribers(title, message);
  }
 
  private IBroadcastService MasterServer { get; set; }
 
  public event Action<string, string> Broadcast;
 
  public void BroadcastMessage(string title, string message)
  {
    MasterServer.BroadcastMessage(title, message);
  }
 
  private void BroadcastMessageToLocalSubscribers(string title, string message)
  {
    var broadcast = Broadcast;
    if (broadcast != null)
    {
      broadcast(title, message);
    }
  }
}
 
static class SlaveServer
{
  public static void Main()
  {
    // connect to master server
    var conn = new ZyanConnection("tcp://localhost:8765/Master");
    var masterServer = conn.CreateProxy<IBroadcastService>();
    Console.WriteLine("Connected to master server: {0}.", conn.ServerUrl);
 
    // start slave server
    var host = new ZyanComponentHost("Slave", 7654);
    host.RegisterComponent<IBroadcastService, RebroadcastService>(
      new RebroadcastService(masterServer));
 
    Console.WriteLine("Slave server started. Press ENTER to quit.");
    Console.ReadLine();
  }
}
 
//----------------------------
// Client project
 
static class Client
{
  public static void Main()
  {
    // connect to slave server
    var conn = new ZyanConnection("tcp://localhost:7654/Slave");
    var slaveServer = conn.CreateProxy<IBroadcastService>();
    Console.WriteLine("Connected to slave server: {0}.", conn.ServerUrl);
 
    // subscribe
    slaveServer.Broadcast += (title, message) =>
    {
      var oldColor = Console.ForegroundColor;
      Console.ForegroundColor = ConsoleColor.Yellow;
      Console.WriteLine("Received: {0}, message: {1}", title, message);
      Console.ForegroundColor = oldColor;
    };
 
    // command loop
    while (true)
    {
      Console.WriteLine("Enter a message to broadcast (empty line to quit):");
 
      var message = Console.ReadLine();
      if (string.IsNullOrEmpty(message))
      {
        break;
      }
 
      Console.WriteLine("Enter a title (optional):");
      var title = Console.ReadLine();
      if (string.IsNullOrEmpty(title))
      {
        title = "Untitled";
      }
 
      slaveServer.BroadcastMessage(title, message);
    }
  }
}
Here is the complete code: https://gist.github.com/yallie/5300068
Feel free to fork and play with it as you wish.