[en] Reconnection detect (solved)

Topics: Technical Support
Mar 17, 2012 at 6:48 PM

ZyanConnection class has the event "Disconnected" to resolve connection loss. How to determine that the client was reconnected on client?

Coordinator
Mar 18, 2012 at 7:44 PM

Hi,

we don't currently have similar event for reconnection, but we can add it. Will it help?

Mar 19, 2012 at 12:34 AM

Yes, it will help. Now I may show message to user "the connection is lost". By subscribing on connected/reconnected event, I will be able to show "connection restored" message or simply write to log. If there is no ready solution, adding the event will solve the problem.

Yes, it will help
Coordinator
Mar 19, 2012 at 11:52 PM

I've added ZyanConnection.Reconnected event.
Here is a tiny example (run it as "test.exe /server" for server and "test.exe" for client):

// csc test.cs /r:Zyan.Communication.dll

using System;
using Zyan.Communication;

public interface ISample
{
  void Hello(string text);
}

public class Sample : ISample
{
  public void Hello(string text)
  {
    Console.WriteLine("Hello, {0}!", text);
  }
}

public class Program
{
  static void Main(string[] args)
  {
    if (args.Length > 0)
    {
      RunServer();
      return;
    }

    RunClient();
  }

  static void RunServer()
  {
    using (var host = new ZyanComponentHost("Test", 8080))
    {
      host.RegisterComponent<ISample, Sample>();
      Console.WriteLine("Server started.");
      Console.ReadLine();
    }
  }

  static void RunClient()
  {
    using (var conn = new ZyanConnection("tcp://localhost:8080/Test"))
    {    
      conn.PollingInterval = TimeSpan.FromSeconds(10);
      conn.PollingEnabled = true;
      conn.NewLogonNeeded += (sender, args) => Console.WriteLine("New logon requested.");
      conn.Reconnected += (sender, args) => Console.WriteLine("Reconnected!");
      conn.Disconnected += (sender, args) =>
      {
        Console.WriteLine("Disconnected! Trying to reconnect...");
        args.Retry = true;
      };

      var proxy = conn.CreateProxy<ISample>();
      var s = "World";

      do
      {
        proxy.Hello(s);
        Console.WriteLine("Enter some words to proceed, ENTER to quit...");
        s = Console.ReadLine();
      }
      while (!string.IsNullOrWhiteSpace(s));
    }
  }
}


How to use it:

1. Run test.exe /server:

2. Run test.exe, type a few words, then close server:

3. Run again test.exe /server:

Hope it helps.