Example #1
0
    //서버로부터 데이터를 받아서 클라이언트에게 전달할 postbox에 넣는 함수이다.

    void ListenData()
    {
        try {
            reader = new StreamReader(stream);

            string line;


            while ((line = reader.ReadLine()) != null)
            {
                Debug.Log("서버에서 온 메시지 : " + line);

                postbox.PushData(line);
            }

            Debug.Log("서버에서 null값을 받음");
        } catch (Exception e) {
            Debug.Log("소켓 익셉션" + e);
        } finally {
            Debug.Log("finally : socket Closed");
            stream.Close();
            clientSocket.Close();
        }
    }
    //서버로부터 serializedData를 받아온다.
    void ListenData()
    {
        try {
            string line;

            //1. 서버에서 데이터(string으로 변환한 serialized 객체)를 읽어옴.
            while ((line = reader.ReadLine()) != null)
            {
                //1-2. userList에 있긴한데, 나간다고한다.
                if (line.Contains("종료"))
                {
                    Debug.Log(line);
                    string[] text      = line.Split(':');
                    string   exit_user = text[1];

                    exitBox.PushData(exit_user);

                    ExitFlag = true;
                }
                //만약에 방을 바꾼 누군가가 새롭게 입장한거라면,
                //내 화면에 그 캐릭터를 생성해 주어야 한다.

                else if (line.Contains("방바꾼뉴비"))
                {
                    Debug.Log("방바꾼 뉴비 입장");
                    string[] temp = line.Split(':');

                    User obj = Deserialize(temp[1]);

                    userList.Add(obj.Id, obj);

                    instanceBox.PushData(obj);
                    //Update 함수에 신호를 줌.
                    InstanceFlag = true;

                    ShowUserList();
                }

                else if (line.Contains("새로운방유저"))
                {
                    Debug.Log("새로운 방유저 리스트 도착");

                    string[] temp = line.Split(':');
                    //str을 쪼개서 배열에 넣은다음
                    string[] u = temp[1].Split(',');

                    // 원래 있던 각 플레이어 정보마다,
                    for (int i = 0; i < u.Length - 1; i++)
                    {
                        // Debug.Log("먼저 접속해있던 유저 아이디들(나누기)"+u[i]);
                        //각 item들(유저 정보 객체)을 역직렬화 시킨후
                        User otherP = Deserialize(u[i]);

                        //해쉬맵에 key=id, value=user객체 형태로 넣음.
                        userList.Add(otherP.Id, otherP);

                        Debug.Log("서버에서 받은 새로운 방 유저: " + otherP.Id);



                        // ICollection 객체에 Key / Value 값들을 리턴함
                        // ICollection keyColl = userList.Keys;

                        // // // ICollection 객체에 각각 저장된 정보를 꺼냄
                        // foreach (string s in keyColl)
                        // {
                        //     Debug.Log("Key = {0}"+ s);
                        // }

                        Debug.Log("방에 있던 유저 아이디: " + otherP.Id);

                        // initiate 시킨다.
                        instanceBox.PushData(otherP);
                        InstanceFlag = true;
                    }

                    ShowUserList();
                }


                else
                {
                    // Debug.Log("서버로부터 위치 데이터를 받아옴...");
                    //2. 역직렬화
                    User obj = Deserialize(line);
                    //3. 누구의 정보인가?
                    // Debug.Log("들어온 정보 from " + obj.Id);

                    //3-1. 만약에 userList를 다까봤는데 이 id 정보가 없다.
                    if (!userList.ContainsKey(obj.Id))
                    {
                        //그러면 뉴비의 입장이라는 뜻이므로
                        //뉴비의 id와 user객체를 해쉬맵에 넣는다.
                        Debug.Log("뉴비입장: " + obj.Id);
                        userList.Add(obj.Id, obj);

                        //그리고 Initiate한다.
                        instanceBox.PushData(obj);

                        //Update 함수에 신호를 줌.
                        InstanceFlag = true;
                    }

                    //4. postbox에 데이터 객체 넣기
                    // 뉴비이든 아니든 뭔가 화면에 변화가 일어났으므로 postbox에 넣는다.
                    postbox.PushData(obj);
                }
            }


            Debug.Log("서버에서 null값을 받음");
        } catch (Exception e) {
            Debug.Log("소켓 익셉션" + e);
        } finally {
            Debug.Log("finally");
        }
    }