這是一種輕巧的客戶端套接字解決方案,您可以在Unity3D或C#項目中使用它

中文說明
快速開始:
//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
}更多示例:
該項目包含:

TCP提供了一個字節流的可靠,有序和錯誤檢查的交付。您必須自己拆分字節,在此框架中,您可以實現iPackage接口以實現此目標。
由於TCP是Bytes協議的AA流,因此用戶應將字節拆分以獲取正確的消息包。創建TCP套接字通道時,必須有一個包裝實例來打包和解開消息。
包裝和解開郵件:一開始,我們定義一個包裝器以拆分字節,當發送消息時,我們在每個消息的頭部添加長度,當接收消息時,我們使用此長度來獲得消息的時間。
UDP提供了數據完整性的校驗和端口號,用於在數據報的源和目的地上解決不同的功能。這意味著您不知道當前的連接狀態,但是包裝已集成。
如果使用UDP連接shold shold dewine send和接收緩衝區大小。
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中有許多例子,這裡有一些:
示例:
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
}
}麻省理工學院許可證
版權(C)[2017] [Hiram]
特此免費授予獲得此軟件副本和相關文檔文件副本(“軟件”)的任何人,以無限制處理該軟件,包括無限制的使用權,複製,複製,修改,合併,合併,發布,分發,分發,分發,訂婚,和/或允許軟件的副本,並允許對以下條件提供以下條件,以下是以下條件。
上述版權通知和此許可通知應包含在軟件的所有副本或大量部分中。
該軟件是“原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,特定目的的適用性和非侵權的保證。在任何情況下,作者或版權持有人都不應對任何索賠,損害賠償或其他責任責任,無論是在合同,侵權的訴訟中還是其他責任,是由軟件,使用或與軟件中的使用或其他交易有關的。