static void Main(string[] args) { SqlDependency.Start(connectionString); // Communicate using WebSockets. IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory(); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8000/RealTimeChartWeb/"); anInputChannel.ResponseReceiverConnected += (x, y) => { Console.WriteLine("Connected client: " + y.ResponseReceiverId); }; anInputChannel.ResponseReceiverDisconnected += (x, y) => { Console.WriteLine("Disconnected client: " + y.ResponseReceiverId); }; // Attach input channel and start listeing. aBroker.AttachDuplexInputChannel(anInputChannel); // Start working thread monitoring the CPU usage. bool aStopWorkingThreadFlag = false; Thread aWorkingThread = new Thread(() => { float usage = 0; while (!aStopWorkingThreadFlag) { getData(ref _refDate, aSerializer, aBroker); Thread.Sleep(100); } }); aWorkingThread.Start(); Console.WriteLine("RealTimeChartWeb is running press ENTER to stop."); Console.ReadLine(); // Wait until the working thread stops. aStopWorkingThreadFlag = true; aWorkingThread.Join(3000); aBroker.DetachDuplexInputChannel(); SqlDependency.Stop(connectionString); // Detach the input channel and stop listening. }
private void Notify(int numberOfTimes, ISerializer serializer, IMessagingSystemFactory messaging, string aBrokerAddress) { IDuplexInputChannel aBrokerInputChannel = messaging.CreateDuplexInputChannel(aBrokerAddress); IDuplexOutputChannel aClient1OutputChannel = messaging.CreateDuplexOutputChannel(aBrokerAddress); IDuplexOutputChannel aClient2OutputChannel = messaging.CreateDuplexOutputChannel(aBrokerAddress); // Specify in the factory that the publisher shall not be notified from its own published events. IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(serializer) { IsPublisherNotified = false }; IDuplexBroker aBroker = aBrokerFactory.CreateBroker(); aBroker.AttachDuplexInputChannel(aBrokerInputChannel); IDuplexBrokerClient aClient1 = aBrokerFactory.CreateBrokerClient(); int aCount = 0; AutoResetEvent aCompletedEvent = new AutoResetEvent(false); aClient1.BrokerMessageReceived += (x, y) => { ++aCount; if (aCount == numberOfTimes) { aCompletedEvent.Set(); } }; aClient1.AttachDuplexOutputChannel(aClient1OutputChannel); IDuplexBrokerClient aClient2 = aBrokerFactory.CreateBrokerClient(); aClient2.AttachDuplexOutputChannel(aClient2OutputChannel); try { var aTimer = new PerformanceTimer(); aTimer.Start(); aClient1.Subscribe("TypeA"); for (int i = 0; i < numberOfTimes; ++i) { // Notify the message. aClient2.SendMessage("TypeA", "Message A"); } aCompletedEvent.WaitOne(); aTimer.Stop(); // Client 2 should not get the notification. Assert.AreEqual(numberOfTimes, aCount); } finally { aClient1.DetachDuplexOutputChannel(); aClient2.DetachDuplexOutputChannel(); aBroker.DetachDuplexInputChannel(); } }