private static async Task DispatchOutMessages() { var lastCleanupTime = DateTime.Now; while (true) { if ((DateTime.Now - lastCleanupTime) > _roomCleanupInterval) // clean up each 10 seconds { lastCleanupTime = DateTime.Now; EchoRoomServer.DropSilentRooms(); } var message = EchoRoomServer.GetMessageToSend(); if (message == null) { Thread.Sleep(0); // better way is to add event to sever that signals queue change and wait on it continue; } var client = message.Client.NetworkId as TcpClient; if (client == null || !client.Connected) { continue; } var writer = new StreamWriter(client.GetStream()) { AutoFlush = true }; await writer.WriteLineAsync($"{DateTime.Now} {message.Author.RoomName} {message.MessageType} {message.Author.Id}:{message.Data}"); } }
public void CtorValidNoThrowExceptions() { var server = new EchoRoomServer(TimeSpan.FromMilliseconds(1)); Assert.AreEqual(0, server.RoomsCount); Assert.AreEqual(0, server.GetMessagesToSendCount()); Assert.IsNull(server.GetMessageToSend()); }
public void GetMessageToSendTestMethod() { var server = new EchoRoomServer(TimeSpan.FromMilliseconds(1000)); var networkId = new object(); var client = server.EnterInRoom("client", networkId, "room"); Message message = server.GetMessageToSend(); Assert.IsNotNull(message); Assert.AreEqual(MessageType.System, message.MessageType); Assert.AreEqual("client", message.Client.Id); Assert.AreEqual("client", message.Author.Id); }