1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DotNetty.Buffers;
- using DotNetty.Transport.Channels;
- namespace nettyCommunication
- {
- public class ClientDemoHandler: ChannelHandlerAdapter
- {
- readonly IByteBuffer initialMessage;
- public static object Message { get; set; }
- public static IChannelHandlerContext CTX { get; set; }
- public ClientDemoHandler()
- {
- MessageUTF8 ms = new MessageUTF8();
- ms.cmd = "R";
- ms.garageID = ClientSettings.GarageID;
- ms.context = "hello wit";
- byte[] byteMessage = JsonByByteToObjectTools.ObjToJsonByte(ms);
- this.initialMessage = Unpooled.Buffer(ClientSettings.Size);
- this.initialMessage.WriteBytes(byteMessage);
- }
- public override void ChannelActive(IChannelHandlerContext context)
- {
- //base.ChannelActive(context);
- CTX = context;
- Console.WriteLine("ChannelActive");
- context.WriteAndFlushAsync(this.initialMessage);
- Console.WriteLine("连接建立,将初始化(注册信息发给服务器)");
- }
- public override void ChannelRead(IChannelHandlerContext context, object message)
- {
- Message = message;
- }
- public override void ChannelReadComplete(IChannelHandlerContext context)
- {
- context.Flush();
- }
- public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
- {
- Console.WriteLine("Exception:"+exception);
- context.CloseAsync();
- }
- }
-
- }
|