コード例 #1
0
ファイル: CommandLine.cs プロジェクト: fabsgc/chat
        public static void Main()
        {
            Chatter       bob = new TextChatter("Bob");
            Chatter       joe = new TextChatter("Joe");
            TopicsManager gt  = new TextGestTopics();

            gt.createTopic("java");
            gt.createTopic("UML");
            gt.listTopics();
            gt.createTopic("jeux");
            gt.listTopics();
            Chatroom cr = gt.joinTopic("jeux");

            cr.join(bob);
            cr.post("Je suis seul ou quoi ?", bob);
            cr.join(joe);
            cr.post("Tiens, salut Joe !", bob);
            cr.post("Toi aussi tu chat sur les forums de jeux pendant les TP, Bob ? ", joe);
        }
コード例 #2
0
        public void startServer()
        {
            TcpListener l = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), port);

            l.Start();

            TopicsManager   gt            = new TextGestTopics();
            List <ChatRoom> ChatRoomsList = new List <ChatRoom>();

            while (true) // Exchanges with the clients
            {
                try
                {
                    TcpClient comm = l.AcceptTcpClient(); // Connection with client
                    Console.WriteLine("Connection established @" + comm);
                    Console.WriteLine("Computing operation");

                    string  response;
                    Message mes = new Message(gt.listTopics());
                    do // Send : Join or Create a Chatroom
                    {  // Send : Names of opened chatrooms
                        MessageConnection.sendMessage(comm.GetStream(), new Message("server", "1 Join a chatroom \n2 Create a chatroom"));
                        MessageConnection.sendMessage(comm.GetStream(), mes);

                        mes      = MessageConnection.getMessage(comm.GetStream());
                        response = mes.msg;
                    } while (!response.Equals("1") && !response.Equals("2"));

                    Chatter chatter = new TextChatter(mes.name); // New user of a chatroom
                    chatterList.Add(chatter);
                    TcpClientList.Add(comm);

                    if (response.Equals("1")) // Join an existing chatroom
                    {
                        do
                        {
                            MessageConnection.sendMessage(comm.GetStream(), new Message("serveur", "Please, choose a chatroom: "));
                            mes = MessageConnection.getMessage(comm.GetStream());
                            if ((!(gt.IsInside(mes.msg))))
                            {
                                Console.WriteLine("This chatroom does not exist.");
                            }
                        } while (mes.msg.Length == 0 || (!(gt.IsInside(mes.msg))));
                    }
                    else // Create a chatroom
                    {
                        do
                        {
                            MessageConnection.sendMessage(comm.GetStream(), new Message("serveur", "Please, give a name to your topic: "));
                            mes = MessageConnection.getMessage(comm.GetStream());
                        } while (mes.msg.Length == 0 || gt.IsInside(mes.msg));

                        gt.createTopic(mes.msg);
                        ChatRoomsList.Add(gt.joinTopic(mes.msg));
                    }

                    foreach (ChatRoom cr in ChatRoomsList)
                    {
                        if (mes.msg.Equals(cr.getTopic()))
                        {
                            cr.join(chatter);                               // Add the chatter in the chatroom
                            new Thread(new Receiver(comm, cr).run).Start(); // Create Threads to listen and send messages
                            TcpClient commGet = l.AcceptTcpClient();

                            chatterList.Add(chatter);
                            TcpClientList.Add(commGet);

                            new Thread(new Receiver(commGet, cr).run).Start();

                            Message join = new Message("server", "Welcome to " + mes.name + " in the " + mes.msg + " chatroom");
                            broadcast(join, cr);//send welcome message
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }