예제 #1
0
        public void SetClientUserID(ChatPacket chatPack)
        {
            User user = BlindChatUtil.ChatPacketToStruct <User>(chatPack);

            this.UserID = user.ID;
            SetOnline((int)UserStat.Online);


            ChatPacketSend(chatPack);
        }
예제 #2
0
        public void ClientUpdateData(ChatPacket chatPack)
        {
            ChatTimeStamp syncTime = BlindChatUtil.ChatPacketToStruct <ChatTimeStamp>(chatPack);


            ClientUpdateUser(syncTime.timeUser);
            ClientUpdateChatRoom(syncTime.timeChatRoom);
            ClientUpdateChatRoomJoined(syncTime.timeChatRoomJoined);
            ClientUpdateMessage(syncTime.timeMessage);

            SendReset();
        }
예제 #3
0
        public void ExecuteNewRoom(ChatPacket chatPack)
        {
            string        timeNow = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            NewRoomStruct newroom = BlindChatUtil.ChatPacketToStruct <NewRoomStruct>(chatPack);

            //DB에 방 정보 추가
            string sql = $"insert into ChatRoom (Name, Time, LastMessageTime) values (\'{newroom.Name}\',\'{timeNow}\', \'{timeNow}\');";

            ExecuteQuery(sql);

            //방금 생성한 방 정보 불러오기(roomid를 알아오기 위해)
            sql = $"select * from ChatRoom where Time = \'{timeNow}\';";
            MySqlDataReader rdr = ExecuteSelect(sql);

            if (rdr.Read())
            {
                int RoomID = int.Parse(rdr["ID"].ToString());
                rdr.Close();
                //방에 속한 사용자를 등록한다.
                for (int i = 0; newroom.UserID[i] != 0; i++)
                {
                    sql = $"insert into ChatRoomJoined (UserID, RoomID, Time) values ({newroom.UserID[i]}, {RoomID}, \'{timeNow}\');";
                    ExecuteQuery(sql);
                }
                //방에 속한 사용자들에게 전송
                ClientUpdateNewRoom(RoomID);

                for (int i = 0; newroom.UserID[i] != 0; i++)
                {
                    ChatMessage message = new ChatMessage();
                    message.RoomID = RoomID;
                    message.UserID = 0;
                    message.Time   = timeNow;

                    sql = $"select * from User where ID = {newroom.UserID[i]}";
                    MySqlDataAdapter adpt = new MySqlDataAdapter(sql, hDB);
                    DataSet          ds   = new DataSet();
                    adpt.Fill(ds);
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        User userInfo = (User)GetStructFromDB <User>(row);
                        message.Message = $"{userInfo.Name}님이 입장하셨습니다.";
                    }
                    byte[]     _data   = BlindNetUtil.StructToByte(message);
                    ChatPacket _packet = BlindChatUtil.ByteToChatPacket(_data, ChatType.Message);
                    SendChatPacketToParticipants(_packet, message.RoomID);

                    //시간을 수정한 메시지를 DB에 등록
                    AddToDBTimeNow <ChatMessage>(_packet);
                }
            }
        }
예제 #4
0
        public void ClientUpdateUser(string Time)//새로 추가된 사용자가 있을 경우 전송
        {
            string          sql = $"select * from User where time > \'{Time}\' order by time asc";
            MySqlDataReader rdr = ExecuteSelect(sql);

            while (rdr.Read())
            {
                User user = (User)GetStructFromDB <User>(rdr);

                byte[] data = BlindNetUtil.StructToByte(user);

                ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.User);
                ChatPacketSend(chatPacket);
            }
            rdr.Close();
        }
예제 #5
0
        public void ExecuteExit(ChatPacket chatPack)
        {
            string         timeNow    = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            ChatRoomJoined roomJoined = BlindChatUtil.ChatPacketToStruct <ChatRoomJoined>(chatPack);

            SendChatPacketToParticipants(chatPack, roomJoined.RoomID);

            string sql = $"delete from ChatRoomJoined where UserID = {roomJoined.UserID} and RoomID = {roomJoined.RoomID};";

            ExecuteQuery(sql);

            ChatMessage message = new ChatMessage();

            message.RoomID = roomJoined.RoomID;
            message.UserID = 0;
            message.Time   = timeNow;

            sql = $"select * from User where ID = {roomJoined.UserID}";

            ChatMessage _message = new ChatMessage();

            message.RoomID = roomJoined.RoomID;
            message.UserID = 0;
            message.Time   = timeNow;

            sql = $"select * from User where ID = {roomJoined.UserID}";
            MySqlDataAdapter adpt = new MySqlDataAdapter(sql, hDB);
            DataSet          ds   = new DataSet();

            adpt.Fill(ds);
            DataRow r = ds.Tables[0].Rows[0];

            User userInfo = (User)GetStructFromDB <User>(r);

            message.Message = $"{userInfo.Name}님이 나갔습니다.";

            byte[]     _data   = BlindNetUtil.StructToByte(message);
            ChatPacket _packet = BlindChatUtil.ByteToChatPacket(_data, ChatType.Message);

            SendChatPacketToParticipants(_packet, message.RoomID);

            //시간을 수정한 메시지를 DB에 등록
            AddToDBTimeNow <ChatMessage>(_packet);
        }
예제 #6
0
        public void ClientUpdateMessage(string Time)
        {
            string sql =
                $"select * from ChatMessage " +
                $"where RoomID in (select roomID from ChatRoomJoined where userID = {UserID}) " +
                $"and Time > \'{Time}\' order by Time asc";

            MySqlDataReader rdr = ExecuteSelect(sql);

            while (rdr.Read())
            {
                ChatMessage message = (ChatMessage)GetStructFromDB <ChatMessage>(rdr);

                byte[] data = BlindNetUtil.StructToByte(message);

                ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.Message);
                ChatPacketSend(chatPacket);
            }
            rdr.Close();
        }
예제 #7
0
        public void ClientUpdateChatRoom(string Time)
        {
            string sql =
                $"select * from ChatRoom " +
                $"where ID in " +
                $"(select RoomID from ChatRoomJoined where UserID={this.UserID}) " +
                $"and Time > \'{Time}\' order by Time asc";
            MySqlDataReader rdr = ExecuteSelect(sql);

            while (rdr.Read())
            {
                ChatRoom room = (ChatRoom)GetStructFromDB <ChatRoom>(rdr);

                byte[] data = BlindNetUtil.StructToByte(room);

                ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.Room);
                ChatPacketSend(chatPacket);
            }
            rdr.Close();
        }
예제 #8
0
        public void MessageToParticipants(ChatPacket chatPack)
        {
            //메시지의 시간을 수정한다.
            string timeNow = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

            ChatMessage message = BlindChatUtil.ChatPacketToStruct <ChatMessage>(chatPack);

            message.Time = timeNow;

            byte[]     data = BlindNetUtil.StructToByte(message);
            ChatPacket pack = BlindChatUtil.ByteToChatPacket(data, ChatType.Message);

            //시간을 수정한 메시지를 DB에 등록
            AddToDBTimeNow <ChatMessage>(pack);

            //방데이터에 최신 메시지 시간 수정
            string sql = $"update ChatRoom set LastMessageTime = \'{message.Time}\' where ID = {message.RoomID}";

            ExecuteQuery(sql);

            //방에 속한 사용자들에게 메시지 전송
            SendChatPacketToParticipants(pack, message.RoomID);
        }
예제 #9
0
        public void ClientRoomsWithRoomID <T>(int RoomID)
        {
            string   sql;
            ChatType Type;

            if (typeof(T) == typeof(ChatRoom))
            {
                sql  = $"select * from ChatRoom where ID = {RoomID};";
                Type = ChatType.Room;
            }
            else if (typeof(T) == typeof(ChatRoomJoined))
            {
                sql  = $"select * from ChatRoomJoined where RoomID ={RoomID}";
                Type = ChatType.RoomJoined;
            }
            else
            {
                return;
            }

            //MySqlDataReader rdr = ExecuteSelect(sql);

            MySqlDataAdapter adpt = new MySqlDataAdapter(sql, hDB);
            DataSet          ds   = new DataSet();

            adpt.Fill(ds);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                T st = (T)GetStructFromDB <T>(row);

                byte[] data = BlindNetUtil.StructToByte(st);

                ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, Type);
                SendChatPacketToParticipants(chatPacket, RoomID);
            }
        }
예제 #10
0
        public void ExecuteInvitation(ChatPacket chatPack)
        {
            ChatRoomJoined roomJoined = BlindChatUtil.ChatPacketToStruct <ChatRoomJoined>(chatPack);

            //사용자 등록
            string timeNow = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            string sql     = $"insert into ChatRoomJoined (UserID, RoomID, Time) values ({roomJoined.UserID},{roomJoined.RoomID},\'{timeNow}\');";

            ExecuteQuery(sql);

            //새로 추가한 사용자 검색
            sql = $"select * from ChatRoomJoined where Time = \'{timeNow}\' and UserID={ roomJoined.UserID }";
            MySqlDataReader rdr = ExecuteSelect(sql);

            byte[] data;
            if (rdr.Read())
            {
                roomJoined = (ChatRoomJoined)GetStructFromDB <ChatRoomJoined>(rdr);
                rdr.Close();

                {
                    sql = $"select * from ChatRoom where ID = {roomJoined.RoomID}";

                    rdr = ExecuteSelect(sql);
                    if (rdr.Read())
                    {
                        ChatRoom room = (ChatRoom)GetStructFromDB <ChatRoom>(rdr);

                        rdr.Close();
                        data = BlindNetUtil.StructToByte(room);

                        ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.Room);
                        SendChatPacketToUser(chatPacket, roomJoined.UserID);
                    }
                }

                //방 인원 전송
                {
                    sql = $"select * from ChatRoomJoined " +
                          $"where RoomID = {roomJoined.RoomID} order by Time asc";

                    MySqlDataAdapter adptr = new MySqlDataAdapter(sql, hDB);
                    DataSet          dst   = new DataSet();
                    adptr.Fill(dst);

                    foreach (DataRow dr in dst.Tables[0].Rows)
                    {
                        ChatRoomJoined chatRoomJoined = (ChatRoomJoined)GetStructFromDB <ChatRoomJoined>(dr);

                        if (chatRoomJoined.UserID != roomJoined.UserID)
                        {
                            data = BlindNetUtil.StructToByte(chatRoomJoined);

                            ChatPacket chatPacket = BlindChatUtil.ByteToChatPacket(data, ChatType.RoomJoined);
                            SendChatPacketToUser(chatPacket, roomJoined.UserID);
                        }
                    }
                    rdr.Close();
                }



                data = BlindNetUtil.StructToByte(roomJoined);
                //방 사용자들에게 전송
                ChatPacket packet = BlindChatUtil.ByteToChatPacket(data, ChatType.RoomJoined);
                SendChatPacketToParticipants(packet, roomJoined.RoomID);

                ChatMessage message = new ChatMessage();
                message.RoomID = roomJoined.RoomID;
                message.UserID = 0;
                message.Time   = timeNow;

                sql = $"select * from User where ID = {roomJoined.UserID}";
                MySqlDataAdapter adpt = new MySqlDataAdapter(sql, hDB);
                DataSet          ds   = new DataSet();
                adpt.Fill(ds);
                DataRow r        = ds.Tables[0].Rows[0];
                User    userInfo = (User)GetStructFromDB <User>(r);

                message.Message = $"{userInfo.Name}님이 입장하셨습니다.";
                byte[]     _data   = BlindNetUtil.StructToByte(message);
                ChatPacket _packet = BlindChatUtil.ByteToChatPacket(_data, ChatType.Message);
                SendChatPacketToParticipants(_packet, message.RoomID);

                //시간을 수정한 메시지를 DB에 등록
                AddToDBTimeNow <ChatMessage>(_packet);
                Console.WriteLine("invite successed");
            }
            else
            {
                Console.WriteLine("cant find the user");
            }
        }