ChatManager.cs
2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 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);
}
}