Beispiel #1
0
        public void Connect(bool waitForInput = true)
        {
            _server = new WebSocketServer(ChatService.ConnectionString)
            {
                RestartAfterListenError = true
            };
            Console.Write("> ");
            _server.Start(socket =>
            {
                socket.OnOpen   = () => AllSockets.Add(socket);
                socket.OnClose  = () => AllSockets.Remove(socket);
                socket.OnBinary = messageData =>
                {
                    var chatMessage = ChatService.DecodeMessage(messageData);
                    if (chatMessage is not null)
                    {
                        if (chatMessage.Message.Contains(AdminMessage.Joined))
                        {
                            Console.WriteLine($"User: {chatMessage.Author} has joined");
                            Console.Write("> ");
                        }
                        else if (chatMessage.Message.Contains(AdminMessage.Left))
                        {
                            Console.WriteLine($"User: {chatMessage.Author} has left");
                            Console.Write("> ");
                        }
                        else
                        {
                            ChatService.DisplayMessage(messageData);
                        }
                    }

                    // Broadcast the message to all listeing clients
                    AllSockets.ToList().ForEach(s => s.Send(messageData));
                };
            });
            IsConnected = true;

            // Send Admin messages to all participants
            if (waitForInput)
            {
                var message = Console.ReadLine();
                while (message != ChatService.QuitKeyword && !ChatService.ShouldExit)
                {
                    foreach (var socket in AllSockets.ToList())
                    {
                        socket.Send(ChatService.EncodeMessage($"ADMIN ] {message}"));
                    }
                    Console.Write("> ");
                    message = Console.ReadLine();
                }
            }
        }