Last active
May 30, 2017 20:29
-
-
Save yallie/437ea4c087725693e1c2 to your computer and use it in GitHub Desktop.
Zyan.com.de demo: using Disconnected and Reconnected events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} | |
} |
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
Server process console:
Client process console: