Skip to content

Instantly share code, notes, and snippets.

@yallie
Last active May 30, 2017 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yallie/437ea4c087725693e1c2 to your computer and use it in GitHub Desktop.
Save yallie/437ea4c087725693e1c2 to your computer and use it in GitHub Desktop.
Zyan.com.de demo: using Disconnected and Reconnected events
// http://zyan.com.de
//
// Compile using: csc test.cs /r:Zyan.Communication.dll
//
// Disconnected/Reconnected events demo usage scenario.
//
// Start up test.exe several times.
// The first process is the server, the rest are clients.
// The server prints polling events to the console once in a second.
//
// Now stop server process.
// The clients start getting OnDisconnected events.
// Every second they print to the console: Disconnected! Retry: 1, 2...
//
// Now run test.exe to restart server process.
// The clients will get reconnected and print Reconnected! on their consoles.
// The server will start getting heartbeat events again.
//
using System;
using Zyan.Communication;
using Zyan.Communication.Protocols.Tcp;
class Program
{
static void Main()
{
try
{
StartServer();
}
catch
{
StartClient();
}
}
static void StartServer()
{
var proto = new TcpDuplexServerProtocolSetup(1234);
using (var host = new ZyanComponentHost("HeartbeatTest", proto))
{
host.PollingEventTracingEnabled = true;
host.ClientHeartbeatReceived += (s, e) =>
Console.WriteLine("Heartbeat received. SessionID: {0}", e.SessionID);
Console.WriteLine("Server started. Press ENTER to quit.");
Console.ReadLine();
}
}
static void StartClient()
{
var proto = new TcpDuplexClientProtocolSetup();
using (var conn = new ZyanConnection(proto.FormatUrl("localhost", 1234, "HeartbeatTest")))
{
conn.NewLogonNeeded += (s, e) => Console.WriteLine("Server restarted, re-login!");
conn.Reconnected += (s, e) => Console.WriteLine("Reconnected!");
conn.Disconnected += (s, e) =>
{
e.Retry = true;
Console.WriteLine("Disconnected! Trying to reconnect: {0}...", e.RetryCount);
};
conn.PollingInterval = TimeSpan.FromSeconds(1);
conn.PollingEnabled = true;
Console.WriteLine("Client started. Press ENTER to quit.");
Console.WriteLine("Try shutting down the server and then firing it up again.");
Console.ReadLine();
}
}
}
@yallie
Copy link
Author

yallie commented Nov 28, 2014

Server process console:

Server started. Press ENTER to quit.
Heartbeat received. SessionID: 9a4bdd35-f8df-4311-8879-4fb6604cd0a3
Heartbeat received. SessionID: 9a4bdd35-f8df-4311-8879-4fb6604cd0a3
Heartbeat received. SessionID: 7e413801-8aad-4d4d-8e08-98f211547e16
Heartbeat received. SessionID: 9a4bdd35-f8df-4311-8879-4fb6604cd0a3
Heartbeat received. SessionID: 7e413801-8aad-4d4d-8e08-98f211547e16
...

Client process console:

Client started. Press ENTER to quit.
Try shutting down the server and then firing it up again.
Disconnected! Trying to reconnect: 0...
Disconnected! Trying to reconnect: 1...
Disconnected! Trying to reconnect: 2...
Disconnected! Trying to reconnect: 3...
Disconnected! Trying to reconnect: 4...
Server restarted, re-login!
Reconnected!

@yallie
Copy link
Author

yallie commented Nov 28, 2014

Using client-side Disconnected and Reconnected events might be tricky.
This is the sample code that demonstrates the usage of these events.

Client-side checklist

  • Add event handlers to ZyanConnection.Disconnected and Reconnected.
  • Make sure Disconnected event handler sets e.Retry to true!
  • Set ZyanConnection.PollingEnabled to true (heartbeat feature is disabled by default).
  • Adjust ZyanConnection.PollingInterval as needed (it defaults to 1 heartbeat per minute).
  • Add an event handler to ZyanConnection.NewLogonNeeded event to handle server restart!

Server-side checklist

  • To enable polling diagnostics, set ZyanComponentHost.PollingEventTracingEnabled to true.
  • Add an event handler to host.ClientHeartbeatReceived event.

Originated from: https://zyan.codeplex.com/discussions/573430

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment