public void ClientReceiveTimeout() { IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory() { ReceiveTimeout = TimeSpan.FromMilliseconds(1000) }; IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("ws://127.0.0.1:8046/"); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8046/"); try { ManualResetEvent aConnectionClosed = new ManualResetEvent(false); anOutputChannel.ConnectionClosed += (x, y) => { EneterTrace.Info("Connection closed."); aConnectionClosed.Set(); }; anInputChannel.StartListening(); anOutputChannel.OpenConnection(); EneterTrace.Info("Connection opened."); // According to set receive timeout the client should get disconnected within 1 second. //aConnectionClosed.WaitOne(); Assert.IsTrue(aConnectionClosed.WaitOne(3000)); } finally { anOutputChannel.CloseConnection(); anInputChannel.StopListening(); } }
public void Setup() { //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug; //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt"); // Generate random number for the port. int aPort = RandomPortGenerator.GenerateInt(); IMessagingSystemFactory anUnderlyingMessaging = new WebSocketMessagingSystemFactory(); IDuplexInputChannel aMessageBusServiceInputChannel = anUnderlyingMessaging.CreateDuplexInputChannel("ws://[::1]:" + aPort + "/Clients/"); IDuplexInputChannel aMessageBusClientInputChannel = anUnderlyingMessaging.CreateDuplexInputChannel("ws://[::1]:" + (aPort + 10) + "/Services/"); myMessageBus = new MessageBusFactory().CreateMessageBus(); myMessageBus.AttachDuplexInputChannels(aMessageBusServiceInputChannel, aMessageBusClientInputChannel); MessagingSystemFactory = new MessageBusMessagingFactory("ws://[::1]:" + aPort + "/Clients/", "ws://[::1]:" + (aPort + 10) + "/Services/", anUnderlyingMessaging) { ConnectTimeout = TimeSpan.FromMilliseconds(3000) }; // Address of the service in the message bus. ChannelId = "Service1_Address"; }
static void Main(string[] args) { // Create broker. IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(); IDuplexBroker aBroker = aBrokerFactory.CreateBroker(); // Communicate using WebSockets. IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory(); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8843/CpuUsage/"); 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); var tokenSource = new CancellationTokenSource(); var sendMonitoringMessages = Task.Run(() => SendMonitoringMessages(aBroker, tokenSource.Token), tokenSource.Token); Console.WriteLine("CpuUsageService is running press ENTER to stop."); Console.ReadLine(); tokenSource.Cancel(); try { sendMonitoringMessages.Wait(); } catch (AggregateException) { } catch (TaskCanceledException) { } // Detach the input channel and stop listening. aBroker.DetachDuplexInputChannel(); }
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. }
public void MaxAmountOfConnections() { IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory() { MaxAmountOfConnections = 2 }; IDuplexOutputChannel anOutputChannel1 = aMessaging.CreateDuplexOutputChannel("ws://127.0.0.1:8049/"); IDuplexOutputChannel anOutputChannel2 = aMessaging.CreateDuplexOutputChannel("ws://127.0.0.1:8049/"); IDuplexOutputChannel anOutputChannel3 = aMessaging.CreateDuplexOutputChannel("ws://127.0.0.1:8049/"); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8049/"); try { ManualResetEvent aConnectionClosed = new ManualResetEvent(false); anOutputChannel3.ConnectionClosed += (x, y) => { EneterTrace.Info("Connection closed."); aConnectionClosed.Set(); }; anInputChannel.StartListening(); anOutputChannel1.OpenConnection(); anOutputChannel2.OpenConnection(); Assert.IsTrue(anOutputChannel1.IsConnected); Assert.IsTrue(anOutputChannel2.IsConnected); Assert.Throws <IOException>(() => anOutputChannel3.OpenConnection()); if (!aConnectionClosed.WaitOne(1000)) { Assert.Fail("Third connection was not closed."); } } finally { anOutputChannel1.CloseConnection(); anOutputChannel2.CloseConnection(); anOutputChannel3.CloseConnection(); anInputChannel.StopListening(); } }
public void OpenServer() { IsClose = true; IsOpen = false; _receiver = _receiverFactory.Value.CreateDuplexTypedMessageReceiver<ChatMessage, ChatMessage>(); _receiver.MessageReceived += OnMessageReceived; _receiver.ResponseReceiverConnected += OnClientConnected; _receiver.ResponseReceiverDisconnected += OnClientDisconnected; // Use webSocket for the communication IMessagingSystemFactory messaging = new WebSocketMessagingSystemFactory(); // Attach input channel and be able to receive request messages and send back response messages. IDuplexInputChannel inputChannel = messaging.CreateDuplexInputChannel("ws://192.168.15.124:8091/"); _receiver.AttachDuplexInputChannel(inputChannel); }