Introducing new features.google protocol 客户端收包
正在显示
3 个修改的文件
包含
14 行增加
和
7 行删除
| @@ -95,9 +95,7 @@ public class IndexController { | @@ -95,9 +95,7 @@ public class IndexController { | ||
| 95 | public BaseResponse<NULLBody> sendProtoBufMsg(@RequestBody GoogleProtocolVO googleProtocolVO){ | 95 | public BaseResponse<NULLBody> sendProtoBufMsg(@RequestBody GoogleProtocolVO googleProtocolVO){ |
| 96 | BaseResponse<NULLBody> res = new BaseResponse(); | 96 | BaseResponse<NULLBody> res = new BaseResponse(); |
| 97 | 97 | ||
| 98 | - for (int i=0 ;i <100 ;i++){ | ||
| 99 | heartbeatClient.sendGoogleProtocolMsg(googleProtocolVO) ; | 98 | heartbeatClient.sendGoogleProtocolMsg(googleProtocolVO) ; |
| 100 | - } | ||
| 101 | 99 | ||
| 102 | // 利用 actuator 来自增 | 100 | // 利用 actuator 来自增 |
| 103 | counterService.increment(Constants.COUNTER_CLIENT_PUSH_COUNT); | 101 | counterService.increment(Constants.COUNTER_CLIENT_PUSH_COUNT); |
| 1 | package com.crossoverjie.netty.action.client.handle; | 1 | package com.crossoverjie.netty.action.client.handle; |
| 2 | 2 | ||
| 3 | -import io.netty.buffer.ByteBuf; | 3 | +import com.crossoverjie.netty.action.common.protocol.BaseResponseProto; |
| 4 | import io.netty.channel.ChannelHandlerContext; | 4 | import io.netty.channel.ChannelHandlerContext; |
| 5 | import io.netty.channel.SimpleChannelInboundHandler; | 5 | import io.netty.channel.SimpleChannelInboundHandler; |
| 6 | -import io.netty.util.CharsetUtil; | ||
| 7 | import org.slf4j.Logger; | 6 | import org.slf4j.Logger; |
| 8 | import org.slf4j.LoggerFactory; | 7 | import org.slf4j.LoggerFactory; |
| 9 | 8 | ||
| @@ -14,7 +13,7 @@ import org.slf4j.LoggerFactory; | @@ -14,7 +13,7 @@ import org.slf4j.LoggerFactory; | ||
| 14 | * Date: 16/02/2018 18:09 | 13 | * Date: 16/02/2018 18:09 |
| 15 | * @since JDK 1.8 | 14 | * @since JDK 1.8 |
| 16 | */ | 15 | */ |
| 17 | -public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> { | 16 | +public class EchoClientHandle extends SimpleChannelInboundHandler<BaseResponseProto.ResponseProtocol> { |
| 18 | 17 | ||
| 19 | private final static Logger LOGGER = LoggerFactory.getLogger(EchoClientHandle.class); | 18 | private final static Logger LOGGER = LoggerFactory.getLogger(EchoClientHandle.class); |
| 20 | 19 | ||
| @@ -47,11 +46,12 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> { | @@ -47,11 +46,12 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> { | ||
| 47 | } | 46 | } |
| 48 | 47 | ||
| 49 | @Override | 48 | @Override |
| 50 | - protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) throws Exception { | 49 | + protected void channelRead0(ChannelHandlerContext channelHandlerContext, BaseResponseProto.ResponseProtocol responseProtocol) throws Exception { |
| 51 | 50 | ||
| 52 | //从服务端收到消息时被调用 | 51 | //从服务端收到消息时被调用 |
| 53 | - LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ; | 52 | + //LOGGER.info("客户端收到消息={}",in.toString(CharsetUtil.UTF_8)) ; |
| 54 | 53 | ||
| 54 | + LOGGER.info("客户端收到消息={}" ,responseProtocol.getResMsg()); | ||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | @Override | 57 | @Override |
netty-action-heartbeat/src/main/java/com/crossoverjie/netty/action/handle/HeartBeatSimpleHandle.java
| @@ -2,6 +2,7 @@ package com.crossoverjie.netty.action.handle; | @@ -2,6 +2,7 @@ package com.crossoverjie.netty.action.handle; | ||
| 2 | 2 | ||
| 3 | import com.crossoverjie.netty.action.common.pojo.CustomProtocol; | 3 | import com.crossoverjie.netty.action.common.pojo.CustomProtocol; |
| 4 | import com.crossoverjie.netty.action.common.protocol.BaseRequestProto; | 4 | import com.crossoverjie.netty.action.common.protocol.BaseRequestProto; |
| 5 | +import com.crossoverjie.netty.action.common.protocol.BaseResponseProto; | ||
| 5 | import com.crossoverjie.netty.action.util.NettySocketHolder; | 6 | import com.crossoverjie.netty.action.util.NettySocketHolder; |
| 6 | import io.netty.buffer.ByteBuf; | 7 | import io.netty.buffer.ByteBuf; |
| 7 | import io.netty.buffer.Unpooled; | 8 | import io.netty.buffer.Unpooled; |
| @@ -59,6 +60,14 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<BaseReque | @@ -59,6 +60,14 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<BaseReque | ||
| 59 | protected void channelRead0(ChannelHandlerContext ctx, BaseRequestProto.RequestProtocol msg) throws Exception { | 60 | protected void channelRead0(ChannelHandlerContext ctx, BaseRequestProto.RequestProtocol msg) throws Exception { |
| 60 | LOGGER.info("收到msg={}", msg.getReqMsg()); | 61 | LOGGER.info("收到msg={}", msg.getReqMsg()); |
| 61 | 62 | ||
| 63 | + if (999 == msg.getRequestId()){ | ||
| 64 | + BaseResponseProto.ResponseProtocol responseProtocol = BaseResponseProto.ResponseProtocol.newBuilder() | ||
| 65 | + .setResponseId(1000) | ||
| 66 | + .setResMsg("服务端响应") | ||
| 67 | + .build(); | ||
| 68 | + ctx.writeAndFlush(responseProtocol) ; | ||
| 69 | + } | ||
| 70 | + | ||
| 62 | //保存客户端与 Channel 之间的关系 | 71 | //保存客户端与 Channel 之间的关系 |
| 63 | //NettySocketHolder.put(CustomProtocolProtocol.getId(),(NioSocketChannel)ctx.channel()) ; | 72 | //NettySocketHolder.put(CustomProtocolProtocol.getId(),(NioSocketChannel)ctx.channel()) ; |
| 64 | } | 73 | } |
-
请 注册 或 登录 后发表评论