using Boo.Lang; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using UnityEngine; using UnityEngine.SocialPlatforms; using ZLog; public class DemoLua : MonoBehaviour { string m_Ip = "192.168.0.55"; int m_Port = 9410; float reconnectTime = 0; private void Start() { // 设置日志的Level,屏蔽Socket的部分日志。设到2以下,查看所有日志 Options.Instance.LogLevel = 3; //Log.Verbose = false; // 在主线程初始化ZLog Log.Debug(""); // 连接服务器 LuaSocketManager.Connect(m_Ip, m_Port); // 发送心跳包消息的测试 StartCoroutine(SendLoop()); } private void Update() { if (Input.GetKeyUp(KeyCode.Space)) { CompositePictureReq(); } if (LuaSocketManager.Update() != ZSocket.ErrorCode.ERR_Success) { // 发生错误,重连处理 if (Time.time > reconnectTime) { Debug.LogError($"Demo Reconnect ErrorCode: {SimpleSocketManager.ErrorCode}"); LuaSocketManager.Connect(m_Ip, m_Port); reconnectTime = Time.time + 1f; } } LuaSocketManager.MsgPacket msgPacket = LuaSocketManager.RecvMessage(); if (msgPacket != null) { Log.Warning($"{msgPacket.msgType}\n{msgPacket.msgData}"); } } private void OnDestroy() { // 断开与服务器的连接 LuaSocketManager.Disconnect(); } [Serializable] public class Rect { public float x; public float y; public float width; public float height; } [Serializable] public class PictureReq { public Rect rect; public string jacketUrl; public string userUrl; } void CompositePictureReq() { byte[] bytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "panda.png")); string base64 = Convert.ToBase64String(bytes); string data = JsonUtility.ToJson(new PictureReq { rect = new Rect(), jacketUrl = "", userUrl = base64 }); LuaSocketManager.Send("COMPOSITE_PICTURE_REQ", Encoding.UTF8.GetBytes(data)); } IEnumerator SendLoop() { int i = 1; //while (i > 0) while (true) { i--; for (int j = 0; j < 1; ++j) { // 发送不需要回复的消息 //Debug.Log($"Demo Send {SimpleSocketManager.ErrorCode}"); //SimpleSocketManager.Send( "HEARTBEAT", @"{""ping"":true}"); // 发送需要回复的RPC消息,结果通过回调获取 //Debug.Log($"Demo Rpc {LuaSocketManager.ErrorCode}"); LuaSocketManager.Send("HEARTBEAT_REQ", Encoding.UTF8.GetBytes(@"{""ping"":true}")); } yield return new WaitForSeconds(1f); //yield return new WaitForEndOfFrame(); } } }