这是一种轻巧的客户端套接字解决方案,您可以在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]
特此免费授予获得此软件副本和相关文档文件副本(“软件”)的任何人,以无限制处理该软件,包括无限制的使用权,复制,复制,修改,合并,合并,发布,分发,分发,分发,订婚,和/或允许软件的副本,并允许对以下条件提供以下条件,以下是以下条件。
上述版权通知和此许可通知应包含在软件的所有副本或大量部分中。
该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,特定目的的适用性和非侵权的保证。在任何情况下,作者或版权持有人都不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权的诉讼中还是其他责任,是由软件,使用或与软件中的使用或其他交易有关的。