가벼운 클라이언트 소켓 솔루션으로 Unity3d 또는 C# Project에서 사용할 수 있습니다.

中文说明
빠른 시작 :
//tcp example
private IPackage package = new PackageExample ( ) ;
private TcpConnection tcp ;
void Init ( )
{
tcp = new TcpConnection ( package ) ;
tcp . OnConnected += OnConnected ;
tcp . OnReceive += OnReceive ;
//...
//...
tcp . Connect ( "127.0.0.1" , 999 ) ;
}
void OnConnected ( )
{
//connect success
tcp . Send ( new byte [ 10 ] ) ; //send message
}
void OnReceive ( byte [ ] bytes )
{
//get message from server
}More example:
이 프로젝트에는 다음이 포함됩니다.

TCP는 바이트 스트림의 신뢰할 수 있고 주문 및 오류 확인 된 전달을 제공합니다. 이 프레임 워크에서이를 달성하기 위해 iPackage 인터페이스를 구현할 수 있습니다.
TCP는 바이트 프로토콜의 AA 스트림이므로 사용자는 올바른 메시지 패키지를 얻기 위해 바이트를 분할해야합니다. TCP 소켓 채널을 만들 때 메시지를 포장하고 포장을 풀 수있는 패키지 인스턴스가 있어야합니다.
포장 및 포장 메시지 : 처음에 우리는 패키지를 분할 바이 테스로 정의하고, 메시지를 보내면 모든 메시지의 헤드에 길이를 추가하고 메시지를받을 때이 길이를 사용하여 메시지가 얼마나 오래 걸리십시오.
UDP는 데이터 무결성을위한 체크섬과 데이터 그램의 소스 및 대상에서 다른 기능을 해결하기위한 포트 번호를 제공합니다. 즉, 현재 Connect 상태를 알지 못하지만 패키지는 통합되었습니다.
UDP 연결을 사용하는 경우 Shold Send and Ever의 버퍼 크기를 정의하십시오.
public int PingTime ;
private Ping p ;
private float timeOut = 1 ;
private float lastTime ;
void Start ( )
{
StartCoroutine ( Ping ( ) ) ;
}
IEnumerator Ping ( )
{
p = new Ping ( "127.0.0.1" ) ;
lastTime = Time . realtimeSinceStartup ;
while ( ! p . isDone && Time . realtimeSinceStartup - lastTime < 1 )
{
yield return null ;
}
PingTime = p . time ;
p . DestroyPing ( ) ;
yield return new WaitForSeconds ( 1 ) ;
StartCoroutine ( Ping ( ) ) ;
}Hisocketexample 프로젝트 또는 Hisocket.unityPackage 에는 많은 예가 있습니다.
Package example:
public class PackageExample : PackageBase
{
protected override void Pack ( BlockBuffer < byte > bytes , Action < byte [ ] > onPacked )
{
//Use int as header
int length = bytes . WritePosition ;
var header = BitConverter . GetBytes ( length ) ;
var newBytes = new BlockBuffer < byte > ( length + header . Length ) ;
//Write header and body to buffer
newBytes . Write ( header ) ;
newBytes . Write ( bytes . Buffer ) ;
//Notice pack funished
onPacked ( newBytes . Buffer ) ;
}
protected override void Unpack ( BlockBuffer < byte > bytes , Action < byte [ ] > onUnpacked )
{
//Because header is int and cost 4 byte
while ( bytes . WritePosition > 4 )
{
int length = BitConverter . ToInt32 ( bytes . Buffer , 0 ) ;
//If receive body
if ( bytes . WritePosition >= 4 + length )
{
bytes . MoveReadPostion ( 4 ) ;
var data = bytes . Read ( length ) ;
//Notice unpack finished
onUnpacked ( data ) ;
bytes . ResetIndex ( ) ;
}
}
}
} TcpConnection tcp ;
void Connect ( )
{
tcp = new TcpConnection ( new PackageExample ( ) ) ;
tcp . OnDisconnected += OnDisconnect ;
tcp . Connect ( "127.0.0.1" , 999 ) ;
tcp . Socket . NoDelay = true ;
tcp . Socket . SendTimeout = 100 ;
tcp . Socket . ReceiveTimeout = 200 ;
//...
// you can add plugin sub from IPlugins
tcp . AddPlugin ( new StatisticalPlugin ( "Statistical" ) ) ; //this plugin calculate how many send
}
void OnDisconnect ( )
{
var length = tcp . SendBuffer . WritePosition ;
Console . WriteLine ( "Still have {0} not send to server when abnormal shutdown" ) ;
var data = tcp . SendBuffer . Read ( length ) ;
tcp . SendBuffer . ResetIndex ( ) ;
//use can handle these data, for example maybe can send next time when connect again
//tcp.Send(data);
} /// <summary>
/// The recommend is use TcpConnection
/// </summary>
class Example3
{
TcpSocket tcp ; //The recommend is use TcpConnection
void Connect ( )
{
tcp = new TcpSocket ( 1024 ) ; //set buffer size
tcp . OnReceiveBytes += OnReceive ;
tcp . Connect ( "127.0.0.1" , 999 ) ;
}
void OnReceive ( byte [ ] bytes )
{
//split bytes here
}
}지원 : [email protected]
MIT 라이센스
저작권 (C) [2017] [Hiram]
이에 따라이 소프트웨어 및 관련 문서 파일 ( "소프트웨어")의 사본을 얻는 사람에게는 허가가 부여됩니다. 소프트웨어의 사용, 복사, 수정, 합병, 배포, 배포, 숭고 및/또는 소프트웨어의 사본을 판매 할 권한을 포함하여 제한없이 소프트웨어를 처리 할 수 있도록 소프트웨어를 제공 할 권한이 없습니다.
위의 저작권 통지 및이 권한 통지는 소프트웨어의 모든 사본 또는 실질적인 부분에 포함되어야합니다.
이 소프트웨어는 상업성, 특정 목적에 대한 적합성 및 비 침해에 대한 보증을 포함하여 명시 적 또는 묵시적 보증없이 "그대로"제공됩니다. 어떠한 경우에도 저자 또는 저작권 보유자는 계약, 불법 행위 또는 기타, 소프트웨어 또는 소프트웨어의 사용 또는 기타 거래에서 발생하는 계약, 불법 행위 또는 기타의 행동에 관계없이 청구, 손해 또는 기타 책임에 대해 책임을지지 않습니다.