I_Jemin

Refactoring Chat App

......@@ -256,11 +256,11 @@ MonoBehaviour:
m_GameObject: {fileID: 1740271}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d74e03325f4c74af5986ac0df47e31a3, type: 3}
m_Script: {fileID: 11500000, guid: 98c5561a51b8e0142ab376064462bf54, type: 3}
m_Name:
m_EditorClassIdentifier:
chat: {fileID: 2036355376}
inputField: {fileID: 1740273}
chatManager: {fileID: 2036355376}
--- !u!1 &257132851
GameObject:
m_ObjectHideFlags: 0
......@@ -628,7 +628,7 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2036355376}
m_MethodName: JoinChatRoom
m_MethodName: JoinRoom
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
......@@ -673,6 +673,151 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 650336557}
--- !u!1 &691573676
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 691573677}
- component: {fileID: 691573680}
- component: {fileID: 691573679}
- component: {fileID: 691573678}
m_Layer: 5
m_Name: InputField
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &691573677
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 691573676}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 2073465268}
- {fileID: 1399557666}
m_Father: {fileID: 1372740361}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &691573678
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 691573676}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 575553740, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 691573679}
m_TextComponent: {fileID: 1399557667}
m_Placeholder: {fileID: 2073465269}
m_ContentType: 0
m_InputType: 0
m_AsteriskChar: 42
m_KeyboardType: 0
m_LineType: 0
m_HideMobileInput: 0
m_CharacterValidation: 0
m_CharacterLimit: 0
m_OnEndEdit:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.InputField+SubmitEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
m_OnValueChanged:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2036355376}
m_MethodName: UpdateHostAddress
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_TypeName: UnityEngine.UI.InputField+OnChangeEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_CustomCaretColor: 0
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
m_Text: 127.0.0.1
m_CaretBlinkRate: 0.85
m_CaretWidth: 1
m_ReadOnly: 0
--- !u!114 &691573679
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 691573676}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -765806418, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Sprite: {fileID: 10911, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
--- !u!222 &691573680
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 691573676}
--- !u!1 &1003989031
GameObject:
m_ObjectHideFlags: 0
......@@ -985,13 +1130,14 @@ RectTransform:
- {fileID: 1763030733}
- {fileID: 650336558}
- {fileID: 257132852}
- {fileID: 691573677}
m_Father: {fileID: 1310784055}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 150, y: 0}
m_SizeDelta: {x: 210.9, y: 0}
m_Pivot: {x: 1, y: 0.5}
--- !u!114 &1372740362
MonoBehaviour:
......@@ -1067,6 +1213,80 @@ MonoBehaviour:
m_FlexibleWidth: -1
m_FlexibleHeight: -1
m_LayoutPriority: 1
--- !u!1 &1399557665
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1399557666}
- component: {fileID: 1399557668}
- component: {fileID: 1399557667}
m_Layer: 5
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1399557666
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1399557665}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 691573677}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: -0.5}
m_SizeDelta: {x: -20, y: -13}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1399557667
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1399557665}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 708705254, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 25
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 40
m_Alignment: 0
m_AlignByGeometry: 0
m_RichText: 0
m_HorizontalOverflow: 1
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: 127.0.0.1
--- !u!222 &1399557668
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1399557665}
--- !u!1 &1443135692
GameObject:
m_ObjectHideFlags: 0
......@@ -1121,9 +1341,9 @@ MonoBehaviour:
m_Bottom: 0
m_ChildAlignment: 0
m_Spacing: 0
m_ChildForceExpandWidth: 0
m_ChildForceExpandWidth: 1
m_ChildForceExpandHeight: 0
m_ChildControlWidth: 0
m_ChildControlWidth: 1
m_ChildControlHeight: 0
--- !u!114 &1443135695
MonoBehaviour:
......@@ -1266,8 +1486,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -75, y: 0}
m_SizeDelta: {x: -150, y: 0}
m_AnchoredPosition: {x: -105.45, y: 0}
m_SizeDelta: {x: -210.9, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1573133660
MonoBehaviour:
......@@ -1587,8 +1807,8 @@ GameObject:
serializedVersion: 5
m_Component:
- component: {fileID: 2036355377}
- component: {fileID: 2036355376}
- component: {fileID: 2036355375}
- component: {fileID: 2036355376}
m_Layer: 0
m_Name: Chat Manager
m_TagString: Untagged
......@@ -1615,15 +1835,15 @@ MonoBehaviour:
m_GameObject: {fileID: 2036355374}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7d91d3622aead48008aca0d637d4ad9b, type: 3}
m_Script: {fileID: 11500000, guid: 8240f60e023c1a0419f8a05cf380e33a, type: 3}
m_Name:
m_EditorClassIdentifier:
m_transport: {fileID: 2036355375}
commentTextPrefab: {fileID: 114312857566104806, guid: b4721aef43e524b91936d391647d3ae1,
m_messageTextPrafab: {fileID: 114555819942375258, guid: b4721aef43e524b91936d391647d3ae1,
type: 2}
commentHolder: {fileID: 1443135693}
m_messageHolder: {fileID: 1443135693}
m_hostAddress: 127.0.0.1
m_port: 50763
m_port: 50666
--- !u!4 &2036355377
Transform:
m_ObjectHideFlags: 0
......@@ -1637,3 +1857,77 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2073465267
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 2073465268}
- component: {fileID: 2073465270}
- component: {fileID: 2073465269}
m_Layer: 5
m_Name: Placeholder
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &2073465268
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2073465267}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 691573677}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: -0.5}
m_SizeDelta: {x: -20, y: -13}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &2073465269
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2073465267}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 708705254, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 0.5}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 25
m_FontStyle: 2
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 40
m_Alignment: 0
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Enter IP Address
--- !u!222 &2073465270
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2073465267}
......
......@@ -21,9 +21,9 @@ GameObject:
- component: {fileID: 224026180460817448}
- component: {fileID: 222343435350097842}
- component: {fileID: 114640140560748198}
- component: {fileID: 114312857566104806}
- component: {fileID: 114555819942375258}
m_Layer: 5
m_Name: Comment
m_Name: Message Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......@@ -79,7 +79,7 @@ MonoBehaviour:
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Sample Text
--- !u!114 &114312857566104806
--- !u!114 &114555819942375258
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
......@@ -87,10 +87,10 @@ MonoBehaviour:
m_GameObject: {fileID: 1351082387170804}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e3c2e0a12ace649978c3c197ab4726a6, type: 3}
m_Script: {fileID: 11500000, guid: f76bbd8dd17bc35488b1ca48227bbca3, type: 3}
m_Name:
m_EditorClassIdentifier:
commentText: {fileID: 114206746021236310}
messageText: {fileID: 114206746021236310}
--- !u!114 &114640140560748198
MonoBehaviour:
m_ObjectHideFlags: 1
......@@ -147,7 +147,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 200, y: 50}
m_SizeDelta: {x: 0, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!224 &224999107880086880
RectTransform:
......
......@@ -3,16 +3,18 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MessageInputField : MonoBehaviour {
public class ChatInputField : MonoBehaviour {
public Chat chat;
public InputField inputField;
public ChatManager chatManager;
void Update () {
void Update()
{
if(Input.GetKeyDown(KeyCode.Return) && !string.IsNullOrEmpty(inputField.text))
{
chat.Send(inputField.text);
chatManager.Send(inputField.text);
inputField.text = string.Empty;
}
}
}
......
fileFormatVersion: 2
guid: 7d91d3622aead48008aca0d637d4ad9b
timeCreated: 1517219122
licenseType: Pro
guid: 98c5561a51b8e0142ab376064462bf54
timeCreated: 1517460942
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
using UnityEngine;
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System;
public class ChatManager : MonoBehaviour {
List<GameObject> messages = new List<GameObject>();
public class Chat : MonoBehaviour
{
public TransportTCP m_transport;
public TransportTCP m_transport; // 네트워크 연결을 담당
public ChatText commentTextPrefab;
public MessageText m_messageTextPrafab; // 말풍선
public Transform commentHolder;
public Transform m_messageHolder; // 말풍선을 붙일곳
public string m_hostAddress = "127.0.0.1";
public int m_port = 50763;
public int m_port = 50666;
private bool m_isServer = false;
private bool m_isHost; // 방장(서버)
// Use this for initialization
void Start()
public void UpdateHostAddress(string newAddress)
{
m_transport.onStateChanged += OnEventHandling;
m_hostAddress = newAddress;
}
IEnumerator UpdateChatting()
// 매프레임마다 패킷큐를 긁어와서 새로운 메시지를 추가
IEnumerator UpdateMessage()
{
while (true)
while(true)
{
byte[] buffer = new byte[1400];
int recvSize = m_transport.Receive(ref buffer, buffer.Length);
if (recvSize > 0)
int recvSize = m_transport.Receive(ref buffer,buffer.Length);
if(recvSize > 0)
{
string message = System.Text.Encoding.UTF8.GetString(buffer);
Debug.Log("Recv data:" + message);
AddComment(message);
Debug.Log("Receive: " + message);
AddMessageText(message);
}
yield return null;
}
}
public void Send(string message)
// 텍스트를 넘겨주면 프리팹을 찍어내서 말풍선을 추가
void AddMessageText(string message)
{
message = "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
MessageText instance = Instantiate(m_messageTextPrafab,m_messageHolder);
m_transport.Send(buffer, buffer.Length);
AddComment(message);
messages.Add(instance.gameObject);
instance.SetUp(message);
}
void AddComment(string message)
{
var newComment = Instantiate(commentTextPrefab, commentHolder);
newComment.SetUp(message);
}
void OnApplicationQuit()
{
if (m_transport != null)
// 방 만들기 (서버 역할하기)
public void CreateRoom()
{
if (m_isServer)
if(m_transport.StartServer(m_port,1))
{
m_transport.StopServer();
m_isHost = true;
StartCoroutine("UpdateMessage");
}
else
{
m_transport.Disconnect();
}
}
}
public void OnEventHandling(NetEventState state)
{
switch (state.type)
{
case NetEventType.Connect:
AddComment("접속");
Debug.Log("접속");
break;
case NetEventType.Disconnect:
Debug.Log("접속 종료");
AddComment("접속 종료");
break;
Debug.LogError("Create a Room Failed");
}
}
public void CreateRoom()
// 클라이언트가 미리 만들어진 방에 가는것
public void JoinRoom()
{
m_transport.StartServer(m_port, 1);
m_isServer = true;
StartCoroutine("UpdateChatting");
}
public void JoinChatRoom()
if(m_transport.Connect(m_hostAddress,m_port))
{
bool ret = m_transport.Connect(m_hostAddress, m_port);
if (ret)
{
StartCoroutine("UpdateChatting");
m_isHost = false;
StartCoroutine("UpdateMessage");
}
else
{
Debug.LogError("Failed");
Debug.LogError("Join Room Failed");
}
}
public void Leave()
{
if (m_isServer == true)
while(messages.Count > 0)
{
var instance = messages[0];
messages.RemoveAt(0);
Destroy(instance);
}
if(m_isHost)
{
m_transport.StopServer();
}
......@@ -127,8 +100,23 @@ public class Chat : MonoBehaviour
m_transport.Disconnect();
}
StopCoroutine("UpdateChatting");
StopCoroutine("UpdateMessage");
}
void OnApplicationQuit()
{
Leave();
}
public void Send(string message)
{
message = "[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);
m_transport.Send(buffer,buffer.Length);
AddMessageText(message);
}
}
......
fileFormatVersion: 2
guid: e3c2e0a12ace649978c3c197ab4726a6
timeCreated: 1517221132
licenseType: Pro
guid: 8240f60e023c1a0419f8a05cf380e33a
timeCreated: 1517461505
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
......@@ -3,12 +3,13 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChatText : MonoBehaviour {
public class MessageText : MonoBehaviour {
public Text messageText;
public Text commentText;
public void SetUp(string message)
{
commentText.text = message;
messageText.text = message;
}
}
......
fileFormatVersion: 2
guid: d74e03325f4c74af5986ac0df47e31a3
timeCreated: 1517244266
licenseType: Pro
guid: f76bbd8dd17bc35488b1ca48227bbca3
timeCreated: 1517460675
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
......@@ -147,6 +147,7 @@ public class TransportTCP : MonoBehaviour
state.type = NetEventType.Connect;
state.result = (isConnected == true) ? NetEventResult.Success : NetEventResult.Failure;
onStateChanged(state);
Debug.Log("Event Handler Called");
}
......