/// <summary> /// 接收到消息 /// </summary> /// <param name="ms"></param> void OnReceivedMessage(byte[] message) { ByteBuffer buffer = new ByteBuffer(message); //放在lua中解析pbid int pbId = IPAddress.NetworkToHostOrder(buffer.ReadShort()); if (pbId == 10000) { if (ConnectObserver.inReconnect) { ConnectObserver.DispatchEvent(Protocal.Reconnected, new ByteBuffer()); if (reconnectAction != null) { reconnectAction(); } } heartBeatService.resetTimeout(); heartBeatService.pingUpdate(); } else { buffer.SeekToBegin(); ConnectObserver.DispatchEvent(Protocal.Message, buffer); heartBeatService.resetTimeout(); } }
internal void pingUpdate() { int protocal = Protocal.PingUpdate; ByteBuffer buffer = new ByteBuffer(); buffer.WriteShort((short)protocal); ConnectObserver.DispatchEvent(protocal, buffer); }
public void UpdateHeartBeat2Server(float deltaTime) { if (heartBeatService != null && client != null && client.Connected) { if (heartBeatService.updateHeartBeat2Server(deltaTime)) { ConnectObserver.DispatchEvent(Protocal.HeartBeat2Server, new ByteBuffer()); } } }
/// <summary> /// 主动从服务器断开连接 /// </summary> public void DisconnectFromServer(int protocal) { StopHeartBeat(); Close(); if (protocal == Protocal.ConnectToGate) { OnRemove(); } if (protocal == 0) { protocal = Protocal.KickOut; } ByteBuffer buffer = new ByteBuffer(); buffer.WriteShort((short)protocal); ConnectObserver.DispatchEvent(protocal, buffer); }
/// <summary> /// 丢失链接 /// </summary> void OnDisconnected(DisType dis, string msg) { if (client != null) { Close(); //关掉客户端链接 int protocal = dis == DisType.Exception ? Protocal.Exception : Protocal.Disconnect; ByteBuffer buffer = new ByteBuffer(); buffer.WriteShort((short)protocal); ConnectObserver.DispatchEvent(protocal, buffer); if (ConnectObserver.inReconnect) { ConnectObserver.inReconnect = false; } Debug.LogWarning(string.Format("Connection was closed:>{0} Distype:>{1}", msg, dis)); } }
/// <summary> /// 连接上服务器 /// </summary> void OnConnect(IAsyncResult asr) { try { outStream = client.GetStream(); client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); if (!ConnectObserver.inReconnect) { if (isGate) { ConnectObserver.DispatchEvent(Protocal.ConnectedToGate, new ByteBuffer()); } else { ConnectObserver.DispatchEvent(Protocal.Connect, new ByteBuffer()); } } } catch (Exception e) { Debug.LogException(e); OnDisconnected(DisType.Exception, e.Message); } }