I_Jemin

Refactoring Chat App

This diff is collapsed. Click to expand it.
......@@ -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 Chat : MonoBehaviour
{
public TransportTCP m_transport;
public ChatText commentTextPrefab;
public Transform commentHolder;
public string m_hostAddress = "127.0.0.1";
public int m_port = 50763;
private bool m_isServer = false;
// Use this for initialization
void Start()
{
m_transport.onStateChanged += OnEventHandling;
}
IEnumerator UpdateChatting()
{
while (true)
{
byte[] buffer = new byte[1400];
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);
}
yield return null;
}
}
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);
AddComment(message);
}
void AddComment(string message)
{
var newComment = Instantiate(commentTextPrefab, commentHolder);
newComment.SetUp(message);
}
void OnApplicationQuit()
{
if (m_transport != null)
{
if (m_isServer)
{
m_transport.StopServer();
}
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;
}
}
public void CreateRoom()
{
m_transport.StartServer(m_port, 1);
m_isServer = true;
StartCoroutine("UpdateChatting");
}
public void JoinChatRoom()
{
bool ret = m_transport.Connect(m_hostAddress, m_port);
if (ret)
{
StartCoroutine("UpdateChatting");
}
else
{
Debug.LogError("Failed");
}
}
public void Leave()
{
if (m_isServer == true)
{
m_transport.StopServer();
}
else
{
m_transport.Disconnect();
}
StopCoroutine("UpdateChatting");
}
public class ChatManager : MonoBehaviour {
List<GameObject> messages = new List<GameObject>();
public TransportTCP m_transport; // 네트워크 연결을 담당
public MessageText m_messageTextPrafab; // 말풍선
public Transform m_messageHolder; // 말풍선을 붙일곳
public string m_hostAddress = "127.0.0.1";
public int m_port = 50666;
private bool m_isHost; // 방장(서버)
public void UpdateHostAddress(string newAddress)
{
m_hostAddress = newAddress;
}
// 매프레임마다 패킷큐를 긁어와서 새로운 메시지를 추가
IEnumerator UpdateMessage()
{
while(true)
{
byte[] buffer = new byte[1400];
int recvSize = m_transport.Receive(ref buffer,buffer.Length);
if(recvSize > 0)
{
string message = System.Text.Encoding.UTF8.GetString(buffer);
Debug.Log("Receive: " + message);
AddMessageText(message);
}
yield return null;
}
}
// 텍스트를 넘겨주면 프리팹을 찍어내서 말풍선을 추가
void AddMessageText(string message)
{
MessageText instance = Instantiate(m_messageTextPrafab,m_messageHolder);
messages.Add(instance.gameObject);
instance.SetUp(message);
}
// 방 만들기 (서버 역할하기)
public void CreateRoom()
{
if(m_transport.StartServer(m_port,1))
{
m_isHost = true;
StartCoroutine("UpdateMessage");
}
else
{
Debug.LogError("Create a Room Failed");
}
}
// 클라이언트가 미리 만들어진 방에 가는것
public void JoinRoom()
{
if(m_transport.Connect(m_hostAddress,m_port))
{
m_isHost = false;
StartCoroutine("UpdateMessage");
}
else
{
Debug.LogError("Join Room Failed");
}
}
public void Leave()
{
while(messages.Count > 0)
{
var instance = messages[0];
messages.RemoveAt(0);
Destroy(instance);
}
if(m_isHost)
{
m_transport.StopServer();
}
else
{
m_transport.Disconnect();
}
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");
}
......