중요: UNet은 지원이 중단된 솔루션이며, 새로운 멀티플레이어 및 네트워킹 솔루션(게임 오브젝트용 넷코드)이 개발 중입니다. 자세한 내용과 다음 단계는 게임 오브젝트용 Unity 넷코드 웹사이트에 있는 정보를 참조하십시오. |
NetworkConnection은 네트워크 연결을 캡슐화하는 고수준 API 클래스입니다. NetworkClient 오브젝트에는 NetworkConnection
이 있고, NetworkServers에는 연결이 각 클라이언트마다 하나씩 여러 개 있습니다. NetworkConnections는 바이트 배열을 전송하거나 직렬화된 오브젝트를 네트워크 메시지로 전송할 수 있습니다.
프로퍼티: | 기능: | |
---|---|---|
hostId | 이 연결의 NetworkTransport hostId입니다. | |
connectionId | 이 연결의 NetworkTransport connectionId입니다. |
|
isReady | 이 연결로 상태 업데이트를 전송할지 제어하는 플래그입니다. | |
lastMessageTime | 이 연결에서 메시지가 마지막으로 수신된 시간입니다. | |
address | 이 연결이 연결된 엔드포인트의 IP 주소입니다. | |
playerControllers | AddPlayer()를 사용하여 추가된 플레이어 집합입니다. | |
clientOwnedObjects | 이 연결이 권한을 보유한 오브젝트 집합입니다. |
NetworkConnection 클래스에는 데이터가 전송 레이어로 전송되거나 전송 레이어로부터 데이터를 수신할 때 호출되는 가상 함수가 있습니다. 이 함수를 통해 NetworkConnection의 특수 버전으로 이 데이터를 검사 또는 수정하거나 심지어는 다른 소스로 전송할 수도 있습니다. 이 함수는 디폴트 동작을 포함해 아래에 나와 있습니다.
public virtual void TransportRecieve(byte[] bytes, int numBytes, int channelId)
{
HandleBytes(bytes, numBytes, channelId);
}
public virtual bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
{
return NetworkTransport.Send(hostId, connectionId, channelId, bytes, numBytes, out error);
}
이 함수는 예를 들어 수신 또는 발신 패킷 콘텐츠의 로그를 작성하는 용도로 사용됩니다. 아래에는 패킷의 첫 50바이트를 콘솔에 기록하는 NetworkConnection에서 파생된 DebugConnection 클래스의 예가 나와 있습니다. 이런 클래스를 사용하려면 NetworkClient 또는 NetworkServer에서 SetNetworkConnectionClass() 함수를 호출해야 합니다.
class DebugConnection : NetworkConnection
{
public override void TransportRecieve(byte[] bytes, int numBytes, int channelId)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < numBytes; i++)
{
var s = String.Format("{0:X2}", bytes[i]);
msg.Append(s);
if (i > 50) break;
}
UnityEngine.Debug.Log("TransportRecieve h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);
HandleBytes(bytes, numBytes, channelId);
}
public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < numBytes; i++)
{
var s = String.Format("{0:X2}", bytes[i]);
msg.Append(s);
if (i > 50) break;
}
UnityEngine.Debug.Log("TransportSend h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);
return NetworkTransport.Send(hostId, connectionId, channelId, bytes, numBytes, out error);
}
}
NetworkConnection
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.