これは軽量のクライアントソケットソリューションであり、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は送信と受信バッファサイズを定義します。
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
}
}サポート:[email protected]
MITライセンス
Copyright(c)[2017] [Hiram]
このソフトウェアと関連するドキュメントファイル(「ソフトウェア」)のコピーを入手して、制限なしにソフトウェアを扱うために、このソフトウェアを制限する権利を含め、ソフトウェアのコピーをコピー、変更、公開、配布、販売する、ソフトウェアのコピーを許可する人を許可する人を許可することを含めて、許可が無料で許可されます。
上記の著作権通知とこの許可通知は、ソフトウェアのすべてのコピーまたはかなりの部分に含まれるものとします。
このソフトウェアは、商品性、特定の目的への適合性、および非侵害の保証を含むがこれらに限定されない、明示的または黙示的なものを保証することなく、「現状のまま」提供されます。いかなる場合でも、著者または著作権所有者は、契約、不法行為、またはその他の訴訟、ソフトウェアまたはソフトウェアの使用またはその他の取引に関連する、またはその他の契約、またはその他の請求、またはその他の責任について責任を負いません。