public void SendMessage(IMessage obj) { if (!MessageDefine.ContainProtoType(obj.GetType())) { AppDebug.Log("协议不存在:" + obj.GetType().ToString()); return; } Message msg = new Message(); //set header msg.Header = new Header(); //set body { byte[] messageBody = obj.ToByteArray(); byte[] body = new byte[2 + messageBody.Length]; using (AppMemoryStream ms = new AppMemoryStream()) { int id = MessageDefine.GetProtoIdByProtoType(obj.GetType()); ms.WriteUShort((ushort)(id)); ms.Write(messageBody, 0, messageBody.Length); body = ms.ToArray(); } msg.Body = ByteString.CopyFrom(body); } //发送消息 int protoId = MessageDefine.GetProtoIdByProtoType(msg.GetType()); SocketClient.Instance.SendMessage(protoId, msg.ToByteArray()); }
public override void Dispose() { m_Player = null; Event.Instance.RemoveListener("OnTcpConnected", OnTcpConnected); SocketEvent.Instance.RemoveListener((ushort)MessageDefine.GetProtoIdByProtoType(typeof(StcUserAuthentication)), OnStcAuthentication); base.Dispose(); }
private void SendToService(Header header, IMessage obj) { Player p = UserManager.Instance.GetPlayer(); if (p.UserId == 0) { AppDebug.Log("无法获取玩家信息,请重新登陆!"); return; } header.UserId = p.UserId; header.Token = p.Token; header.TokenExpiredTime = p.TokenExpireTime; Message msg = new Message(); msg.Header = header; {//set body byte[] bodyData = obj.ToByteArray(); byte[] body = new byte[2 + bodyData.Length]; using (AppMemoryStream ms = new AppMemoryStream()) { int id = MessageDefine.GetProtoIdByProtoType(obj.GetType()); ms.WriteUShort((ushort)(id)); ms.Write(bodyData, 0, bodyData.Length); body = ms.ToArray(); } msg.Body = ByteString.CopyFrom(body); } //发送消息 int protoId = MessageDefine.GetProtoIdByProtoType(msg.GetType()); SocketClient.Instance.SendMessage(protoId, msg.ToByteArray()); }
void Start() { SocketEvent.Instance.AddListener((ushort)MessageDefine.GetProtoIdByProtoType(typeof(StcUserEnter)), OnStcUserEnter); UserManager.Instance.LoginByAccount("1003", "123456"); }
public UserManager() { Event.Instance.AddListener("OnTcpConnected", OnTcpConnected); SocketEvent.Instance.AddListener((ushort)MessageDefine.GetProtoIdByProtoType(typeof(StcUserAuthentication)), OnStcAuthentication); }