-
Notifications
You must be signed in to change notification settings - Fork 9
网络模块
Hiram edited this page Apr 4, 2018
·
3 revisions
网络模块基于HiSockethttps://github.com/hiramtan/HiSocket_unity
示例代码如下:
private ITcp _tcp;
private IPackage _packer = new Packer();
// Use this for initialization
void Start()
{
_tcp = new TcpConnection(_packer);
_tcp.ReceiveEvent += OnReceive;
Connect();
}
void Update()
{
_tcp.Run();
}
void Connect()
{
_tcp.Connect("127.0.0.1", 7777);
}
// Update is called once per frame
void OnReceive(byte[] bytes)
{
Debug.Log("receive msg: " + BitConverter.ToInt32(bytes, 0));
}
public class Packer : IPackage
{
public void Unpack(IByteArray reader, Queue<byte[]> receiveQueue)
{
//add your unpack logic here
}
public void Pack(Queue<byte[]> sendQueue, IByteArray writer)
{
var bytesWaitToPack = sendQueue.Dequeue();
writer.Write(bytesWaitToPack);
}
}