示例#1
0
文件: Program.cs 项目: iarestov/echo
        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}");
            }
        }
示例#2
0
        public void CtorValidNoThrowExceptions()
        {
            var server = new EchoRoomServer(TimeSpan.FromMilliseconds(1));

            Assert.AreEqual(0, server.RoomsCount);
            Assert.AreEqual(0, server.GetMessagesToSendCount());
            Assert.IsNull(server.GetMessageToSend());
        }
示例#3
0
        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);
        }