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 +
7 +public class ChatManager : MonoBehaviour {
8 +
9 + List<GameObject> messages = new List<GameObject>();
6 10
7 -public class Chat : MonoBehaviour 11 + public TransportTCP m_transport; // 네트워크 연결을 담당
8 -{
9 - public TransportTCP m_transport;
10 12
11 - public ChatText commentTextPrefab; 13 + public MessageText m_messageTextPrafab; // 말풍선
12 14
13 - public Transform commentHolder; 15 + public Transform m_messageHolder; // 말풍선을 붙일곳
14 16
15 public string m_hostAddress = "127.0.0.1"; 17 public string m_hostAddress = "127.0.0.1";
16 18
17 - public int m_port = 50763; 19 + public int m_port = 50666;
18 20
19 - private bool m_isServer = false; 21 + private bool m_isHost; // 방장(서버)
20 22
21 - // Use this for initialization 23 + public void UpdateHostAddress(string newAddress)
22 - void Start()
23 { 24 {
24 - m_transport.onStateChanged += OnEventHandling; 25 + m_hostAddress = newAddress;
25 } 26 }
26 27
27 - 28 + // 매프레임마다 패킷큐를 긁어와서 새로운 메시지를 추가
28 - IEnumerator UpdateChatting() 29 + IEnumerator UpdateMessage()
29 { 30 {
30 - while (true) 31 + while(true)
31 { 32 {
32 byte[] buffer = new byte[1400]; 33 byte[] buffer = new byte[1400];
33 34
34 - int recvSize = m_transport.Receive(ref buffer, buffer.Length); 35 + int recvSize = m_transport.Receive(ref buffer,buffer.Length);
35 - if (recvSize > 0) 36 +
37 + if(recvSize > 0)
36 { 38 {
37 string message = System.Text.Encoding.UTF8.GetString(buffer); 39 string message = System.Text.Encoding.UTF8.GetString(buffer);
38 - Debug.Log("Recv data:" + message); 40 + Debug.Log("Receive: " + message);
39 - 41 + AddMessageText(message);
40 - AddComment(message);
41 } 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 -
52 - byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
53 51
54 - m_transport.Send(buffer, buffer.Length); 52 + messages.Add(instance.gameObject);
55 - 53 + instance.SetUp(message);
56 - AddComment(message);
57 } 54 }
58 55
59 56
60 - void AddComment(string message) 57 + // 방 만들기 (서버 역할하기)
61 - { 58 + public void CreateRoom()
62 - var newComment = Instantiate(commentTextPrefab, commentHolder);
63 - newComment.SetUp(message);
64 - }
65 - void OnApplicationQuit()
66 - {
67 - if (m_transport != null)
68 { 59 {
69 - if (m_isServer) 60 + if(m_transport.StartServer(m_port,1))
70 { 61 {
71 - m_transport.StopServer(); 62 + m_isHost = true;
63 + StartCoroutine("UpdateMessage");
72 } 64 }
73 else 65 else
74 { 66 {
75 - m_transport.Disconnect(); 67 + Debug.LogError("Create a Room Failed");
76 - }
77 - }
78 - }
79 -
80 - public void OnEventHandling(NetEventState state)
81 - {
82 - switch (state.type)
83 - {
84 - case NetEventType.Connect:
85 - AddComment("접속");
86 - Debug.Log("접속");
87 - break;
88 -
89 - case NetEventType.Disconnect:
90 - Debug.Log("접속 종료");
91 - AddComment("접속 종료");
92 - break;
93 } 68 }
94 } 69 }
95 70
96 - 71 + // 클라이언트가 미리 만들어진 방에 가는것
97 - public void CreateRoom() 72 + public void JoinRoom()
98 { 73 {
99 - m_transport.StartServer(m_port, 1); 74 + if(m_transport.Connect(m_hostAddress,m_port))
100 - m_isServer = true;
101 - StartCoroutine("UpdateChatting");
102 - }
103 -
104 - public void JoinChatRoom()
105 { 75 {
106 - bool ret = m_transport.Connect(m_hostAddress, m_port); 76 + m_isHost = false;
107 - 77 + StartCoroutine("UpdateMessage");
108 - if (ret)
109 - {
110 - StartCoroutine("UpdateChatting");
111 } 78 }
112 else 79 else
113 { 80 {
114 - Debug.LogError("Failed"); 81 + Debug.LogError("Join Room Failed");
115 } 82 }
116 } 83 }
117 84
118 -
119 public void Leave() 85 public void Leave()
120 { 86 {
121 - if (m_isServer == true) 87 + while(messages.Count > 0)
88 + {
89 + var instance = messages[0];
90 + messages.RemoveAt(0);
91 + Destroy(instance);
92 + }
93 +
94 + if(m_isHost)
122 { 95 {
123 m_transport.StopServer(); 96 m_transport.StopServer();
124 } 97 }
...@@ -127,8 +100,23 @@ public class Chat : MonoBehaviour ...@@ -127,8 +100,23 @@ public class Chat : MonoBehaviour
127 m_transport.Disconnect(); 100 m_transport.Disconnect();
128 } 101 }
129 102
130 - StopCoroutine("UpdateChatting"); 103 + StopCoroutine("UpdateMessage");
131 } 104 }
132 105
106 + void OnApplicationQuit()
107 + {
108 + Leave();
109 + }
110 +
111 + public void Send(string message)
112 + {
113 + message = "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message;
114 +
115 + byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
116 +
117 + m_transport.Send(buffer,buffer.Length);
118 +
119 + AddMessageText(message);
120 + }
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
......