private void WSClient__Opened(object sender, EventArgs e) { if (m_wsQClient.IsConnected && ((AsyncTcpSession)sender).LocalEndPoint == m_wsQClient?.LocalEndPoint) { Task.Factory.StartNew(() => { while (true) { if (m_wsQClient != null && m_wsQClient.IsConnected) { OMDC_HeartBeatReq HB = new OMDC_HeartBeatReq(); HB.head.PktSize = BitConverter.GetBytes((UInt16)40); HB.head.MsgCount[0] = 1; HB.head.Filter[0] = 0; HB.head.SeqNum = BitConverter.GetBytes(num); HB.head.SendTime = BitConverter.GetBytes(GETTick() * 100); HB.msgType.MsgSize = BitConverter.GetBytes((UInt16)24); HB.msgType.MsgType = BitConverter.GetBytes((UInt16)1000); byte[] request = SerHelper.Serialize(HB); m_wsQClient.Send(request); Console.WriteLine($"heartbeat_{num}:{ByteConvertUtil.byteToHexStr(request)}"); } num++; Thread.Sleep(1500); } }); } }
/// <summary> /// 接收返回 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WSClient_DataReceived(object sender, DataEventArgs e) { byte[] receiveBuffer = e.Data.Take(e.Length).ToArray(); string resp = ByteConvertUtil.byteToHexStr(receiveBuffer); Console.WriteLine(resp); }
/// <summary> /// 接收返回 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WSClient_DataReceived(object sender, DataEventArgs e) { try { byte[] receiveBuffer = new byte[e.Length]; Buffer.BlockCopy(e.Data, 0, receiveBuffer, 0, e.Length); lock (body) { body = ByteConvertUtil.BufferCopy(body, receiveBuffer); } do { ushort whole_length = BitConverter.ToUInt16(body, 0);//消息长度 Message Length byte[] header = new byte[haderSize]; Buffer.BlockCopy(body, 0, header, 0, header.Length); //long seqNum = BitConverter.ToInt64(header, 12);//消息的序列号 ushort msgCount = BitConverter.ToUInt16(header, 20); //消息数包 if (msgCount == 0) //refer to heartbeat { Console.WriteLine($"heartbeat!"); RemoveBodyManyLength(whole_length); break; } } while (body.Length > 38); } catch (Exception ex) { _logger.LogError(ex, $"[Recv] WSClient_DataReceived:发生异常"); body = new byte[0]; return; } }