作者 crossoverJie

:sparkles: Introducing new features.google protocol 客户端收包

... ... @@ -95,9 +95,7 @@ public class IndexController {
public BaseResponse<NULLBody> sendProtoBufMsg(@RequestBody GoogleProtocolVO googleProtocolVO){
BaseResponse<NULLBody> res = new BaseResponse();
for (int i=0 ;i <100 ;i++){
heartbeatClient.sendGoogleProtocolMsg(googleProtocolVO) ;
}
// 利用 actuator 来自增
counterService.increment(Constants.COUNTER_CLIENT_PUSH_COUNT);
... ...
package com.crossoverjie.netty.action.client.handle;
import io.netty.buffer.ByteBuf;
import com.crossoverjie.netty.action.common.protocol.BaseResponseProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -14,7 +13,7 @@ import org.slf4j.LoggerFactory;
* Date: 16/02/2018 18:09
* @since JDK 1.8
*/
public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> {
public class EchoClientHandle extends SimpleChannelInboundHandler<BaseResponseProto.ResponseProtocol> {
private final static Logger LOGGER = LoggerFactory.getLogger(EchoClientHandle.class);
... ... @@ -47,11 +46,12 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> {
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) throws Exception {
protected void channelRead0(ChannelHandlerContext channelHandlerContext, BaseResponseProto.ResponseProtocol responseProtocol) throws Exception {
//从服务端收到消息时被调用
LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ;
//LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ;
LOGGER.info("客户端收到消息={}" ,responseProtocol.getResMsg());
}
@Override
... ...
... ... @@ -2,6 +2,7 @@ package com.crossoverjie.netty.action.handle;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import com.crossoverjie.netty.action.common.protocol.BaseRequestProto;
import com.crossoverjie.netty.action.common.protocol.BaseResponseProto;
import com.crossoverjie.netty.action.util.NettySocketHolder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
... ... @@ -59,6 +60,14 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<BaseReque
protected void channelRead0(ChannelHandlerContext ctx, BaseRequestProto.RequestProtocol msg) throws Exception {
LOGGER.info("收到msg={}", msg.getReqMsg());
if (999 == msg.getRequestId()){
BaseResponseProto.ResponseProtocol responseProtocol = BaseResponseProto.ResponseProtocol.newBuilder()
.setResponseId(1000)
.setResMsg("服务端响应")
.build();
ctx.writeAndFlush(responseProtocol) ;
}
//保存客户端与 Channel 之间的关系
//NettySocketHolder.put(CustomProtocolProtocol.getId(),(NioSocketChannel)ctx.channel()) ;
}
... ...