Esempio n. 1
0
        public void run()
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, ChatSetting.port);

            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            newsock.Bind(ipep);
            newsock.Listen(1000);

            while (true)
            {
                Socket socket = newsock.Accept();
                Console.WriteLine("接受一個新連線!");
                ChatSocket client = new ChatSocket(socket);
                try
                {
                    clientList.Add(client);
                    client.newListener(processMsgComeIn);
                }
                catch
                {
                }
//                clientList.Remove(client);
            }
            //	  newsock.Close();
        }
Esempio n. 2
0
 public void setChatroomIndex(ChatSocket socket, int index)
 {
     foreach (Member m in memberList)
     {
         if (m.socket.Equals(socket))
         {
             m.chatroomIndex = index;
         }
         break;
     }
 }
Esempio n. 3
0
 public string getNameBySocket(ChatSocket sock)
 {
     for (int i = 0; i < clientList.Count; i++)
     {
         if (clientList[i] == sock)
         {
             return(userList[i]);
         }
     }
     return(null);
 }
Esempio n. 4
0
 public void broadCast(byte[] msg, ChatSocket socket)
 {
     Console.WriteLine("廣播訊息給 " + msg + " 線上使用者共" + clientList.Count + "個人!");
     foreach (ChatSocket client in clientList)
     {
         if (!client.isDead && !client.Equals(socket))
         {
             Console.WriteLine("Send to " + client.remoteEndPoint.ToString() + ":" + msg);
             client.send(msg);
         }
     }
 }
Esempio n. 5
0
 public Member(ChatSocket socket, int chatroomIndex)
 {
     this.socket        = socket;
     this.chatroomIndex = chatroomIndex;
 }
Esempio n. 6
0
        public void processMsgComeIn(byte[] msg, ChatSocket socket)
        {
            Packet packet = new Packet(msg);

            switch (packet.getCommand())
            {
            case Packet.Commands.ReportName:
                string name = packet.getReportNameData();
                Console.WriteLine("收到使用者:" + name);
                Console.WriteLine(socket.socket.RemoteEndPoint.ToString());
                userList.Insert(clientList.IndexOf(socket), name);
                sendUserList();
                break;

            case Packet.Commands.ChatRequest:
                Console.WriteLine("enter chatrequest");
                info.memberList.Add(new ChatroomInfo.Member(socket, packet.getChatroomIndex()));
                foreach (string user in packet.getChatRequestData())
                {
                    Console.WriteLine(user);
                    info.memberList.Add(new ChatroomInfo.Member(getSocketByName(user), -1));
                }
                chatroomList.Add(info);
                int serverindex = chatroomList.IndexOf(info);

                foreach (string user in packet.getChatRequestData())
                {
                    List <string> sendto = new List <string>();
                    for (int i = 0; i < info.memberList.Count; i++)
                    {
                        if (user != getNameBySocket(info.memberList[i].socket))
                        {
                            Console.WriteLine("sendto" + user + "List" + getNameBySocket(info.memberList[i].socket));
                            sendto.Add(getNameBySocket(info.memberList[i].socket));
                        }
                    }
                    Packet packet1 = new Packet();

                    packet1.makePacketChatRequest(sendto);
                    packet1.changeChatroomIndex(serverindex);
                    byte[] byte1 = packet1.getPacket();
                    getSocketByName(user).send(byte1);
                }

                break;

            case Packet.Commands.RegisterChatroom:
                Console.WriteLine("enter RegisterChatroom");
                int socketindex   = packet.getChatroomIndex();
                int chatroomindex = packet.getRegisterChatroomData();
                chatroomList[chatroomindex].setChatroomIndex(socket, socketindex);
                break;
            // case Packet.Commands.LeaveChatroom:
            //  break;

            case Packet.Commands.TextMessage:
                int chatroomIndex = packet.getChatroomIndex();
                //packet.changeChatroomIndex(2);
                break;

            case Packet.Commands.LogOut:
                Console.WriteLine(userList[clientList.IndexOf(socket)] + "登出");
                userList.RemoveAt(clientList.IndexOf(socket));
                clientList.Remove(socket);
                socket.close();
                break;
            }
        }