Last active
December 28, 2015 15:59
-
-
Save yallie/7525678 to your computer and use it in GitHub Desktop.
RX observable example with Zyan.
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
// Compile this code using: | |
// | |
// csc rxtest.cs /r:Zyan.Communication.dll /r:System.Reactive.Core.dll /r:System.Reactive.Linq.dll /r:System.Reactive.Interfaces.dll /r:System.Runtime.dll /r:System.Threading.Tasks.dll | |
// | |
// First run — starts server. | |
// Second run — starts client. | |
using System; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Reactive; | |
using System.Reactive.Disposables; | |
using System.Reactive.Linq; | |
using Zyan.Communication; | |
using Zyan.Communication.Protocols.Ipc; | |
struct Program | |
{ | |
const string IpcPortName = "AnyValidFilename"; | |
const string ZyanHostName = "SampleService"; | |
static void Main() | |
{ | |
try | |
{ | |
RunServer(); | |
} | |
catch // can't start two servers on the same IPC port | |
{ | |
RunClient(); | |
} | |
} | |
// ------------------------------- Shared code -------- | |
public interface ISampleService | |
{ | |
event EventHandler RandomEvent; | |
void RaiseRandomEvents(); | |
} | |
// ------------------------------- Client code -------- | |
static void RunClient() | |
{ | |
var protocol = new IpcBinaryClientProtocolSetup(); | |
var url = protocol.FormatUrl(IpcPortName, ZyanHostName); | |
using (var conn = new ZyanConnection(url, protocol)) | |
{ | |
Console.WriteLine("Connected to server. Press ENTER to quit."); | |
// create a proxy and an ordinal event handler | |
var proxy = conn.CreateProxy<ISampleService>(); | |
proxy.RandomEvent += (s, e) => Console.WriteLine("Random event ocured."); | |
// create RX observable with throttle option | |
var observable = Observable.FromEventPattern<EventArgs>(proxy, "RandomEvent").Throttle(TimeSpan.FromSeconds(1)); | |
observable.Subscribe(e => | |
{ | |
Console.WriteLine("Occured later than 1 second after the last event."); | |
}); | |
// let server start sending events to our proxy | |
proxy.RaiseRandomEvents(); | |
Console.ReadLine(); | |
} | |
} | |
// ------------------------------- Server code -------- | |
static void RunServer() | |
{ | |
var protocol = new IpcBinaryServerProtocolSetup(IpcPortName); | |
using (var host = new ZyanComponentHost(ZyanHostName, protocol)) | |
{ | |
// register a singleton | |
host.RegisterComponent<ISampleService, SampleService>(new SampleService()); | |
Console.WriteLine("Server started. Press ENTER to quit."); | |
Console.ReadLine(); | |
} | |
} | |
internal class SampleService : ISampleService | |
{ | |
public event EventHandler RandomEvent; | |
public void RaiseRandomEvents() | |
{ | |
ThreadPool.QueueUserWorkItem(x => | |
{ | |
var random = new Random(); | |
for (int i = 0; i < 10; i++) | |
{ | |
Thread.Sleep(400 + random.Next(1000)); | |
var randomEvent = RandomEvent; | |
if (randomEvent != null) | |
{ | |
RandomEvent(null, EventArgs.Empty); | |
} | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment