เป็นโซลูชันซ็อกเก็ตไคลเอนต์น้ำหนักเบาคุณสามารถใช้ในโครงการ 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 เป็นสตรีม 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
ลิขสิทธิ์ (c) [2017] [Hiram]
ได้รับอนุญาตโดยไม่ต้องเสียค่าใช้จ่ายสำหรับบุคคลใด ๆ ที่ได้รับสำเนาซอฟต์แวร์นี้และไฟล์เอกสารที่เกี่ยวข้อง ("ซอฟต์แวร์") เพื่อจัดการในซอฟต์แวร์โดยไม่มีการ จำกัด รวมถึง แต่ไม่ จำกัด เฉพาะสิทธิ์ในการใช้สำเนาดัดแปลงผสานเผยแพร่เผยแพร่
ประกาศลิขสิทธิ์ข้างต้นและประกาศการอนุญาตนี้จะรวมอยู่ในสำเนาทั้งหมดหรือส่วนสำคัญของซอฟต์แวร์
ซอฟต์แวร์มีให้ "ตามสภาพ" โดยไม่มีการรับประกันใด ๆ ไม่ว่าโดยชัดแจ้งหรือโดยนัยรวมถึง แต่ไม่ จำกัด เฉพาะการรับประกันความสามารถในการค้าการออกกำลังกายสำหรับวัตถุประสงค์เฉพาะและการไม่เข้าร่วม ไม่ว่าในกรณีใดผู้เขียนหรือผู้ถือลิขสิทธิ์จะต้องรับผิดชอบต่อการเรียกร้องความเสียหายหรือความรับผิดอื่น ๆ ไม่ว่าจะเป็นการกระทำของสัญญาการละเมิดหรืออื่น ๆ ที่เกิดขึ้นจากหรือเกี่ยวข้องกับซอฟต์แวร์หรือการใช้งานหรือการติดต่ออื่น ๆ ในซอฟต์แวร์