コード例 #1
0
        public void Broadcast(Message message)
        {
            DatabaseTools DBtools = new DatabaseTools();

            if (message.Recipient == 0)
            {
                string output = JsonConvert.SerializeObject(message);
                DBtools.AddMessage(message);
                foreach (var user in Server.ConnectedUsers)
                {
                    if (user.TcpClient.Connected && !user.Name.Equals(message.SentByName))
                    {
                        try
                        {
                            var Writer = new NetworkIO(user.TcpClient);
                            Writer.Write(output);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            else
            {
                string output = JsonConvert.SerializeObject(message);

                for (int i = 0; i < ConnectedUsers.Count(); i++)
                {
                    if (message.Recipient == ConnectedUsers[i].Id && ConnectedUsers[i].TcpClient.Connected)
                    {
                        var Writer = new NetworkIO(ConnectedUsers[i].TcpClient);
                        Writer.Write(output);
                        DBtools.AddMessage(message);
                        break;
                    }
                    else
                    {
                    }
                }
            }
        }
コード例 #2
0
        public void Run()
        {
            try
            {
                string message = "";


                while (!message.ToLower().Equals("quit")) //TODO: EVERYONE GETS THROWN OUT IF SOMEONE WRITES QUIT
                {
                    var Reader = new NetworkIO(this.TcpClient);
                    var Writer = new NetworkIO(this.TcpClient);
                    message = Reader.ReadString();

                    switch (message)
                    {
                    case "changename":
                        Writer.Write("Vilket namn vill du byta till?");
                        this.Name = Reader.ReadString();
                        break;

                    case "myname":
                        Writer.Write("Ditt nuvarande chattnamn är: " + this.Name);
                        break;

                    case "whoishere":
                        foreach (var user in Server.ConnectedUsers)
                        {
                            if (user != this)
                            {
                                Writer.Write(user.Id + " " + user.Name);
                            }
                            else
                            {
                            }
                        }
                        break;

                    case "w":
                        Writer.Write("0 Alla i chatrummet");
                        foreach (var user in Server.ConnectedUsers)
                        {
                            if (user != this)
                            {
                                Writer.Write(user.Id + " " + user.Name);
                            }
                            else
                            {
                            }
                        }
                        Writer.Write("Vem vill du viska till? Ange ID enligt listan ovan.");
                        string whisperTo = Reader.ReadString();
                        try
                        {
                            this.WritingTo = Convert.ToInt32(whisperTo);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Writer.Write("Något gick fel. Endast siffror är tillåtna.");
                        }

                        break;

                    case "help":
                        Writer.Write("\nw: Ändra mottagare av dina meddelanden\nmyname: Visar ditt nuvarande chattnamn\nchangename: Ändra ditt chattnamn\nwhoishere: Listar alla chattdeltagare\n");
                        break;

                    default:
                        var tmpMsg = new Message(this.Id, message, this.WritingTo, this.Name);
                        MessageQueue.AddMessage(tmpMsg);
                        Console.WriteLine(message);
                        break;
                    }
                }

                TcpClient.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }