unity3d 异步socket msgpack序列化
程序员文章站
2022-03-26 14:41:37
...
using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using mutils;
using scopely.msgpacksharp;
using structs;
public class AsyncSocket
{
//单例模式
private static AsyncSocket instance;
public Socket clientSocket;
public string host = "127.0.0.1";
public int hostPort = 23456;
public static AsyncSocket GetInstance ()
{
if (instance == null) {
instance = new AsyncSocket ();
}
return instance;
}
public AsyncSocket ()
{
//创建Socket对象
clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//服务器IP地址
IPAddress ipAddress = IPAddress.Parse (host);
//这是一个异步的建立连接,当连接建立成功时调用connectCallback方法
IAsyncResult result = clientSocket.BeginConnect (ipAddress, hostPort, new AsyncCallback (connectCallback), clientSocket);
//当连接超过5秒还没成功表示超时,非必须
//这段succ变量,偶尔在Mono里报错,实在看不出问题,去掉也没问题
// bool succ = result.AsyncWaitHandle.WaitOne ();
// if (!succ) {
// //超时
// Debug.Log ("time out ");
// closeSocket ();
// }
}
private void sendCallback (IAsyncResult asyncConnect)
{
int bytesSent = clientSocket.EndSend (asyncConnect);
Debug.Log ("sendCallback: sent" + bytesSent);
closeSocket ();
}
private void connectCallback (IAsyncResult asyncConnect)
{
Debug.Log ("EndConnect");
clientSocket.EndConnect (asyncConnect);
businessComm ();
}
private void businessComm ()
{
if (!clientSocket.Connected) {
Debug.Log ("businessComm clientSocket.Connected false");
closeSocket ();
}
MemoryStream memStream = new MemoryStream (128);
memStream.Write (SocketUtils.Int32ToByte (12345), 0, 4);
memStream.Write (SocketUtils.Int32ToByte (78901), 0, 4);
MyMessage message = new MyMessage ();
message.MyNumber = 99;
message.MyString = "abcde";
byte[] datas = MsgPackSerializer.SerializeObject (message);
memStream.Write (SocketUtils.Int32ToByte (datas.Length), 0, 4);
memStream.Write (datas, 0, datas.Length);
byte[] byteData = memStream.GetBuffer ();
clientSocket.BeginSend (byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback (sendCallback), clientSocket);
}
//关闭Socket
public void closeSocket ()
{
Debug.Log ("Socket Closed");
clientSocket.Close ();
}
}
触发器调用:
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "Player") {
AsyncSocket.GetInstance ();
}
}
转载于:https://my.oschina.net/u/1431106/blog/215144
上一篇: three.js学习笔记(一)
下一篇: 3D数学基础:图形与游戏开发笔记1