package com.snowcattle.game.common.socket.client;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* Created by jiangwenping on 17/1/24.
*/
public class ProtoClientHandler extends ChannelInboundHandlerAdapter {
private final ByteBuf firstMessage;
public ProtoClientHandler() {
firstMessage = Unpooled.buffer(1024);
byte[] sendString = "hello world".getBytes();
firstMessage.writeInt(sendString.length);
firstMessage.writeBytes(sendString);
// firstMessage.writeInt(234);
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(firstMessage);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.write(msg);
System.out.println(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}