I_Jemin

Refactoring Chat App

This diff is collapsed. Click to expand it.
...@@ -21,9 +21,9 @@ GameObject: ...@@ -21,9 +21,9 @@ GameObject:
21 - component: {fileID: 224026180460817448} 21 - component: {fileID: 224026180460817448}
22 - component: {fileID: 222343435350097842} 22 - component: {fileID: 222343435350097842}
23 - component: {fileID: 114640140560748198} 23 - component: {fileID: 114640140560748198}
24 - - component: {fileID: 114312857566104806} 24 + - component: {fileID: 114555819942375258}
25 m_Layer: 5 25 m_Layer: 5
26 - m_Name: Comment 26 + m_Name: Message Text
27 m_TagString: Untagged 27 m_TagString: Untagged
28 m_Icon: {fileID: 0} 28 m_Icon: {fileID: 0}
29 m_NavMeshLayer: 0 29 m_NavMeshLayer: 0
...@@ -79,7 +79,7 @@ MonoBehaviour: ...@@ -79,7 +79,7 @@ MonoBehaviour:
79 m_VerticalOverflow: 0 79 m_VerticalOverflow: 0
80 m_LineSpacing: 1 80 m_LineSpacing: 1
81 m_Text: Sample Text 81 m_Text: Sample Text
82 ---- !u!114 &114312857566104806 82 +--- !u!114 &114555819942375258
83 MonoBehaviour: 83 MonoBehaviour:
84 m_ObjectHideFlags: 1 84 m_ObjectHideFlags: 1
85 m_PrefabParentObject: {fileID: 0} 85 m_PrefabParentObject: {fileID: 0}
...@@ -87,10 +87,10 @@ MonoBehaviour: ...@@ -87,10 +87,10 @@ MonoBehaviour:
87 m_GameObject: {fileID: 1351082387170804} 87 m_GameObject: {fileID: 1351082387170804}
88 m_Enabled: 1 88 m_Enabled: 1
89 m_EditorHideFlags: 0 89 m_EditorHideFlags: 0
90 - m_Script: {fileID: 11500000, guid: e3c2e0a12ace649978c3c197ab4726a6, type: 3} 90 + m_Script: {fileID: 11500000, guid: f76bbd8dd17bc35488b1ca48227bbca3, type: 3}
91 m_Name: 91 m_Name:
92 m_EditorClassIdentifier: 92 m_EditorClassIdentifier:
93 - commentText: {fileID: 114206746021236310} 93 + messageText: {fileID: 114206746021236310}
94 --- !u!114 &114640140560748198 94 --- !u!114 &114640140560748198
95 MonoBehaviour: 95 MonoBehaviour:
96 m_ObjectHideFlags: 1 96 m_ObjectHideFlags: 1
...@@ -147,7 +147,7 @@ RectTransform: ...@@ -147,7 +147,7 @@ RectTransform:
147 m_AnchorMin: {x: 0, y: 0} 147 m_AnchorMin: {x: 0, y: 0}
148 m_AnchorMax: {x: 0, y: 0} 148 m_AnchorMax: {x: 0, y: 0}
149 m_AnchoredPosition: {x: 0, y: 0} 149 m_AnchoredPosition: {x: 0, y: 0}
150 - m_SizeDelta: {x: 200, y: 50} 150 + m_SizeDelta: {x: 0, y: 50}
151 m_Pivot: {x: 0.5, y: 0.5} 151 m_Pivot: {x: 0.5, y: 0.5}
152 --- !u!224 &224999107880086880 152 --- !u!224 &224999107880086880
153 RectTransform: 153 RectTransform:
......
...@@ -3,16 +3,18 @@ using System.Collections.Generic; ...@@ -3,16 +3,18 @@ using System.Collections.Generic;
3 using UnityEngine; 3 using UnityEngine;
4 using UnityEngine.UI; 4 using UnityEngine.UI;
5 5
6 -public class MessageInputField : MonoBehaviour { 6 +public class ChatInputField : MonoBehaviour {
7 7
8 - public Chat chat;
9 public InputField inputField; 8 public InputField inputField;
9 + public ChatManager chatManager;
10 10
11 - void Update () { 11 + void Update()
12 + {
12 if(Input.GetKeyDown(KeyCode.Return) && !string.IsNullOrEmpty(inputField.text)) 13 if(Input.GetKeyDown(KeyCode.Return) && !string.IsNullOrEmpty(inputField.text))
13 { 14 {
14 - chat.Send(inputField.text); 15 + chatManager.Send(inputField.text);
15 inputField.text = string.Empty; 16 inputField.text = string.Empty;
16 } 17 }
17 } 18 }
19 +
18 } 20 }
......
1 fileFormatVersion: 2 1 fileFormatVersion: 2
2 -guid: 7d91d3622aead48008aca0d637d4ad9b 2 +guid: 98c5561a51b8e0142ab376064462bf54
3 -timeCreated: 1517219122 3 +timeCreated: 1517460942
4 -licenseType: Pro 4 +licenseType: Free
5 MonoImporter: 5 MonoImporter:
6 externalObjects: {} 6 externalObjects: {}
7 serializedVersion: 2 7 serializedVersion: 2
......
1 -using UnityEngine; 1 +using System.Collections;
2 -using System;
3 -using System.Collections;
4 using System.Collections.Generic; 2 using System.Collections.Generic;
3 +using UnityEngine;
5 using System.Net; 4 using System.Net;
5 +using System;
6 6
7 -public class Chat : MonoBehaviour 7 +public class ChatManager : MonoBehaviour {
8 -{ 8 +
9 - public TransportTCP m_transport; 9 + List<GameObject> messages = new List<GameObject>();
10 - 10 +
11 - public ChatText commentTextPrefab; 11 + public TransportTCP m_transport; // 네트워크 연결을 담당
12 - 12 +
13 - public Transform commentHolder; 13 + public MessageText m_messageTextPrafab; // 말풍선
14 - 14 +
15 - public string m_hostAddress = "127.0.0.1"; 15 + public Transform m_messageHolder; // 말풍선을 붙일곳
16 - 16 +
17 - public int m_port = 50763; 17 + public string m_hostAddress = "127.0.0.1";
18 - 18 +
19 - private bool m_isServer = false; 19 + public int m_port = 50666;
20 - 20 +
21 - // Use this for initialization 21 + private bool m_isHost; // 방장(서버)
22 - void Start() 22 +
23 - { 23 + public void UpdateHostAddress(string newAddress)
24 - m_transport.onStateChanged += OnEventHandling; 24 + {
25 - } 25 + m_hostAddress = newAddress;
26 - 26 + }
27 - 27 +
28 - IEnumerator UpdateChatting() 28 + // 매프레임마다 패킷큐를 긁어와서 새로운 메시지를 추가
29 - { 29 + IEnumerator UpdateMessage()
30 - while (true) 30 + {
31 - { 31 + while(true)
32 - byte[] buffer = new byte[1400]; 32 + {
33 - 33 + byte[] buffer = new byte[1400];
34 - int recvSize = m_transport.Receive(ref buffer, buffer.Length); 34 +
35 - if (recvSize > 0) 35 + int recvSize = m_transport.Receive(ref buffer,buffer.Length);
36 - { 36 +
37 - string message = System.Text.Encoding.UTF8.GetString(buffer); 37 + if(recvSize > 0)
38 - Debug.Log("Recv data:" + message); 38 + {
39 - 39 + string message = System.Text.Encoding.UTF8.GetString(buffer);
40 - AddComment(message); 40 + Debug.Log("Receive: " + message);
41 - } 41 + AddMessageText(message);
42 - 42 + }
43 - yield return null; 43 + yield return null;
44 - } 44 + }
45 - } 45 + }
46 - 46 +
47 - 47 + // 텍스트를 넘겨주면 프리팹을 찍어내서 말풍선을 추가
48 - public void Send(string message) 48 + void AddMessageText(string message)
49 - { 49 + {
50 - message = "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message; 50 + MessageText instance = Instantiate(m_messageTextPrafab,m_messageHolder);
51 - 51 +
52 - byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message); 52 + messages.Add(instance.gameObject);
53 - 53 + instance.SetUp(message);
54 - m_transport.Send(buffer, buffer.Length); 54 + }
55 - 55 +
56 - AddComment(message); 56 +
57 - } 57 + // 방 만들기 (서버 역할하기)
58 - 58 + public void CreateRoom()
59 - 59 + {
60 - void AddComment(string message) 60 + if(m_transport.StartServer(m_port,1))
61 - { 61 + {
62 - var newComment = Instantiate(commentTextPrefab, commentHolder); 62 + m_isHost = true;
63 - newComment.SetUp(message); 63 + StartCoroutine("UpdateMessage");
64 - } 64 + }
65 - void OnApplicationQuit() 65 + else
66 - { 66 + {
67 - if (m_transport != null) 67 + Debug.LogError("Create a Room Failed");
68 - { 68 + }
69 - if (m_isServer) 69 + }
70 - { 70 +
71 - m_transport.StopServer(); 71 + // 클라이언트가 미리 만들어진 방에 가는것
72 - } 72 + public void JoinRoom()
73 - else 73 + {
74 - { 74 + if(m_transport.Connect(m_hostAddress,m_port))
75 - m_transport.Disconnect(); 75 + {
76 - } 76 + m_isHost = false;
77 - } 77 + StartCoroutine("UpdateMessage");
78 - } 78 + }
79 - 79 + else
80 - public void OnEventHandling(NetEventState state) 80 + {
81 - { 81 + Debug.LogError("Join Room Failed");
82 - switch (state.type) 82 + }
83 - { 83 + }
84 - case NetEventType.Connect: 84 +
85 - AddComment("접속"); 85 + public void Leave()
86 - Debug.Log("접속"); 86 + {
87 - break; 87 + while(messages.Count > 0)
88 - 88 + {
89 - case NetEventType.Disconnect: 89 + var instance = messages[0];
90 - Debug.Log("접속 종료"); 90 + messages.RemoveAt(0);
91 - AddComment("접속 종료"); 91 + Destroy(instance);
92 - break; 92 + }
93 - } 93 +
94 - } 94 + if(m_isHost)
95 - 95 + {
96 - 96 + m_transport.StopServer();
97 - public void CreateRoom() 97 + }
98 - { 98 + else
99 - m_transport.StartServer(m_port, 1); 99 + {
100 - m_isServer = true; 100 + m_transport.Disconnect();
101 - StartCoroutine("UpdateChatting"); 101 + }
102 - } 102 +
103 - 103 + StopCoroutine("UpdateMessage");
104 - public void JoinChatRoom() 104 + }
105 - { 105 +
106 - bool ret = m_transport.Connect(m_hostAddress, m_port); 106 + void OnApplicationQuit()
107 - 107 + {
108 - if (ret) 108 + Leave();
109 - { 109 + }
110 - StartCoroutine("UpdateChatting"); 110 +
111 - } 111 + public void Send(string message)
112 - else 112 + {
113 - { 113 + message = "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message;
114 - Debug.LogError("Failed"); 114 +
115 - } 115 + byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
116 - } 116 +
117 - 117 + m_transport.Send(buffer,buffer.Length);
118 - 118 +
119 - public void Leave() 119 + AddMessageText(message);
120 - { 120 + }
121 - if (m_isServer == true)
122 - {
123 - m_transport.StopServer();
124 - }
125 - else
126 - {
127 - m_transport.Disconnect();
128 - }
129 -
130 - StopCoroutine("UpdateChatting");
131 - }
132 -
133 121
134 } 122 }
......
1 fileFormatVersion: 2 1 fileFormatVersion: 2
2 -guid: e3c2e0a12ace649978c3c197ab4726a6 2 +guid: 8240f60e023c1a0419f8a05cf380e33a
3 -timeCreated: 1517221132 3 +timeCreated: 1517461505
4 -licenseType: Pro 4 +licenseType: Free
5 MonoImporter: 5 MonoImporter:
6 externalObjects: {} 6 externalObjects: {}
7 serializedVersion: 2 7 serializedVersion: 2
......
...@@ -3,12 +3,13 @@ using System.Collections.Generic; ...@@ -3,12 +3,13 @@ using System.Collections.Generic;
3 using UnityEngine; 3 using UnityEngine;
4 using UnityEngine.UI; 4 using UnityEngine.UI;
5 5
6 -public class ChatText : MonoBehaviour { 6 +public class MessageText : MonoBehaviour {
7 +
8 + public Text messageText;
7 9
8 - public Text commentText;
9 public void SetUp(string message) 10 public void SetUp(string message)
10 { 11 {
11 - commentText.text = message; 12 + messageText.text = message;
12 } 13 }
13 14
14 } 15 }
......
1 fileFormatVersion: 2 1 fileFormatVersion: 2
2 -guid: d74e03325f4c74af5986ac0df47e31a3 2 +guid: f76bbd8dd17bc35488b1ca48227bbca3
3 -timeCreated: 1517244266 3 +timeCreated: 1517460675
4 -licenseType: Pro 4 +licenseType: Free
5 MonoImporter: 5 MonoImporter:
6 externalObjects: {} 6 externalObjects: {}
7 serializedVersion: 2 7 serializedVersion: 2
......
...@@ -147,6 +147,7 @@ public class TransportTCP : MonoBehaviour ...@@ -147,6 +147,7 @@ public class TransportTCP : MonoBehaviour
147 state.type = NetEventType.Connect; 147 state.type = NetEventType.Connect;
148 state.result = (isConnected == true) ? NetEventResult.Success : NetEventResult.Failure; 148 state.result = (isConnected == true) ? NetEventResult.Success : NetEventResult.Failure;
149 149
150 + onStateChanged(state);
150 Debug.Log("Event Handler Called"); 151 Debug.Log("Event Handler Called");
151 } 152 }
152 153
......