Toggle navigation
Toggle navigation
This project
Loading...
Sign in
I_Jemin
/
dotnet-Network-Programming-with-Unity
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
I_Jemin
2018-02-01 19:10:20 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
51bacae8e8464c28217923542146a93766b82cd3
51bacae8
1 parent
793b1582
Refactoring Chat App
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
89 additions
and
97 deletions
Network Chatting/Assets/Main.unity
Network Chatting/Assets/Comment.prefab → Network Chatting/Assets/Message Text.prefab
Network Chatting/Assets/Comment.prefab.meta → Network Chatting/Assets/Message Text.prefab.meta
Network Chatting/Assets/Scripts/MessageInputField.cs → Network Chatting/Assets/Scripts/ChatInputField.cs
Network Chatting/Assets/Scripts/Chat.cs.meta → Network Chatting/Assets/Scripts/ChatInputField.cs.meta
Network Chatting/Assets/Scripts/Chat.cs → Network Chatting/Assets/Scripts/ChatManager.cs
Network Chatting/Assets/Scripts/ChatText.cs.meta → Network Chatting/Assets/Scripts/ChatManager.cs.meta
Network Chatting/Assets/Scripts/ChatText.cs → Network Chatting/Assets/Scripts/MessageText.cs
Network Chatting/Assets/Scripts/MessageInputField.cs.meta → Network Chatting/Assets/Scripts/MessageText.cs.meta
Network Chatting/Assets/Scripts/TransportTCP.cs
Network Chatting/Assets/Main.unity
View file @
51bacae
This diff is collapsed. Click to expand it.
Network Chatting/Assets/
Commen
t.prefab
→
Network Chatting/Assets/
Message Tex
t.prefab
View file @
51bacae
...
...
@@ -21,9 +21,9 @@ GameObject:
-
component
:
{
fileID
:
224026180460817448
}
-
component
:
{
fileID
:
222343435350097842
}
-
component
:
{
fileID
:
114640140560748198
}
-
component
:
{
fileID
:
114
312857566104806
}
-
component
:
{
fileID
:
114
555819942375258
}
m_Layer
:
5
m_Name
:
Commen
t
m_Name
:
Message Tex
t
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
&114
312857566104806
---
!u!114
&114
555819942375258
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
:
comment
Text
:
{
fileID
:
114206746021236310
}
message
Text
:
{
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
:
20
0
,
y
:
50
}
m_SizeDelta
:
{
x
:
0
,
y
:
50
}
m_Pivot
:
{
x
:
0.5
,
y
:
0.5
}
---
!u!224
&224999107880086880
RectTransform
:
...
...
Network Chatting/Assets/
Commen
t.prefab.meta
→
Network Chatting/Assets/
Message Tex
t.prefab.meta
View file @
51bacae
File moved
Network Chatting/Assets/Scripts/
Message
InputField.cs
→
Network Chatting/Assets/Scripts/
Chat
InputField.cs
View file @
51bacae
...
...
@@ -3,16 +3,18 @@ using System.Collections.Generic;
using
UnityEngine
;
using
UnityEngine.UI
;
public
class
Message
InputField
:
MonoBehaviour
{
public
class
Chat
InputField
:
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
);
chat
Manager
.
Send
(
inputField
.
text
);
inputField
.
text
=
string
.
Empty
;
}
}
}
...
...
Network Chatting/Assets/Scripts/Chat.cs.meta
→
Network Chatting/Assets/Scripts/Chat
InputField
.cs.meta
View file @
51bacae
fileFormatVersion: 2
guid:
7d91d3622aead48008aca0d637d4ad9b
timeCreated: 1517
21912
2
licenseType:
Pro
guid:
98c5561a51b8e0142ab376064462bf54
timeCreated: 1517
46094
2
licenseType:
Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
...
...
Network Chatting/Assets/Scripts/Chat.cs
→
Network Chatting/Assets/Scripts/Chat
Manager
.cs
View file @
51bacae
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
Create
Room
()
// 클라이언트가 미리 만들어진 방에 가는것
public
void
Join
Room
()
{
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
);
}
}
...
...
Network Chatting/Assets/Scripts/Chat
Text
.cs.meta
→
Network Chatting/Assets/Scripts/Chat
Manager
.cs.meta
View file @
51bacae
fileFormatVersion: 2
guid:
e3c2e0a12ace649978c3c197ab4726a6
timeCreated: 1517
221132
licenseType:
Pro
guid:
8240f60e023c1a0419f8a05cf380e33a
timeCreated: 1517
461505
licenseType:
Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
...
...
Network Chatting/Assets/Scripts/
Chat
Text.cs
→
Network Chatting/Assets/Scripts/
Message
Text.cs
View file @
51bacae
...
...
@@ -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
)
{
comment
Text
.
text
=
message
;
message
Text
.
text
=
message
;
}
}
...
...
Network Chatting/Assets/Scripts/Message
InputField
.cs.meta
→
Network Chatting/Assets/Scripts/Message
Text
.cs.meta
View file @
51bacae
fileFormatVersion: 2
guid:
d74e03325f4c74af5986ac0df47e31
a3
timeCreated: 1517
244266
licenseType:
Pro
guid:
f76bbd8dd17bc35488b1ca48227bbc
a3
timeCreated: 1517
460675
licenseType:
Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
...
...
Network Chatting/Assets/Scripts/TransportTCP.cs
View file @
51bacae
...
...
@@ -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"
);
}
...
...
Please
register
or
login
to post a comment