作者 crossoverJie

:sparkles: Introducing new features.修改为发送字符串

@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON; @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
4 import com.crossoverjie.netty.action.client.init.CustomerHandleInitializer; 4 import com.crossoverjie.netty.action.client.init.CustomerHandleInitializer;
5 import com.crossoverjie.netty.action.common.pojo.CustomProtocol; 5 import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
6 import io.netty.bootstrap.Bootstrap; 6 import io.netty.bootstrap.Bootstrap;
  7 +import io.netty.buffer.ByteBuf;
  8 +import io.netty.buffer.Unpooled;
7 import io.netty.channel.ChannelFuture; 9 import io.netty.channel.ChannelFuture;
8 import io.netty.channel.ChannelFutureListener; 10 import io.netty.channel.ChannelFutureListener;
9 import io.netty.channel.EventLoopGroup; 11 import io.netty.channel.EventLoopGroup;
@@ -66,4 +68,17 @@ public class HeartbeatClient { @@ -66,4 +68,17 @@ public class HeartbeatClient {
66 LOGGER.info("客户端手动发消息成功={}", JSON.toJSONString(customProtocol))); 68 LOGGER.info("客户端手动发消息成功={}", JSON.toJSONString(customProtocol)));
67 69
68 } 70 }
  71 + /**
  72 + * 发送消息字符串
  73 + *
  74 + * @param msg
  75 + */
  76 + public void sendStringMsg(String msg) {
  77 + ByteBuf message = Unpooled.buffer(msg.getBytes().length) ;
  78 + message.writeBytes(msg.getBytes()) ;
  79 + ChannelFuture future = channel.writeAndFlush(message);
  80 + future.addListener((ChannelFutureListener) channelFuture ->
  81 + LOGGER.info("客户端手动发消息成功={}", msg));
  82 +
  83 + }
69 } 84 }
@@ -7,7 +7,6 @@ import com.crossoverjie.netty.action.common.constant.Constants; @@ -7,7 +7,6 @@ import com.crossoverjie.netty.action.common.constant.Constants;
7 import com.crossoverjie.netty.action.common.enums.StatusEnum; 7 import com.crossoverjie.netty.action.common.enums.StatusEnum;
8 import com.crossoverjie.netty.action.common.pojo.CustomProtocol; 8 import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
9 import com.crossoverjie.netty.action.common.res.BaseResponse; 9 import com.crossoverjie.netty.action.common.res.BaseResponse;
10 -import com.crossoverjie.netty.action.common.util.RandomUtil;  
11 import io.swagger.annotations.ApiOperation; 10 import io.swagger.annotations.ApiOperation;
12 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.boot.actuate.metrics.CounterService; 12 import org.springframework.boot.actuate.metrics.CounterService;
@@ -58,4 +57,27 @@ public class IndexController { @@ -58,4 +57,27 @@ public class IndexController {
58 res.setDataBody(sendMsgResVO) ; 57 res.setDataBody(sendMsgResVO) ;
59 return res ; 58 return res ;
60 } 59 }
  60 +
  61 + /**
  62 + * 向服务端发消息 字符串
  63 + * @param msg
  64 + * @return
  65 + */
  66 + @ApiOperation("客户端发送消息,字符串")
  67 + @RequestMapping("sendStringMsg")
  68 + @ResponseBody
  69 + public BaseResponse<SendMsgResVO> sendStringMsg(@RequestBody String msg){
  70 + BaseResponse<SendMsgResVO> res = new BaseResponse();
  71 + heartbeatClient.sendStringMsg(msg) ;
  72 +
  73 + // 利用 actuator 来自增
  74 + counterService.increment(Constants.COUNTER_CLIENT_PUSH_COUNT);
  75 +
  76 + SendMsgResVO sendMsgResVO = new SendMsgResVO() ;
  77 + sendMsgResVO.setMsg("OK") ;
  78 + res.setCode(StatusEnum.SUCCESS.getCode()) ;
  79 + res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
  80 + res.setDataBody(sendMsgResVO) ;
  81 + return res ;
  82 + }
61 } 83 }
1 package com.crossoverjie.netty.action.client.handle; 1 package com.crossoverjie.netty.action.client.handle;
2 2
3 -import com.crossoverjie.netty.action.client.util.SpringBeanFactory;  
4 -import com.crossoverjie.netty.action.common.pojo.CustomProtocol;  
5 import io.netty.buffer.ByteBuf; 3 import io.netty.buffer.ByteBuf;
6 -import io.netty.buffer.Unpooled;  
7 -import io.netty.channel.ChannelFutureListener;  
8 import io.netty.channel.ChannelHandlerContext; 4 import io.netty.channel.ChannelHandlerContext;
9 import io.netty.channel.SimpleChannelInboundHandler; 5 import io.netty.channel.SimpleChannelInboundHandler;
10 -import io.netty.handler.timeout.IdleState;  
11 -import io.netty.handler.timeout.IdleStateEvent;  
12 import io.netty.util.CharsetUtil; 6 import io.netty.util.CharsetUtil;
13 import org.slf4j.Logger; 7 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
@@ -29,7 +23,7 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> { @@ -29,7 +23,7 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> {
29 @Override 23 @Override
30 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { 24 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
31 25
32 - if (evt instanceof IdleStateEvent){ 26 + /*if (evt instanceof IdleStateEvent){
33 IdleStateEvent idleStateEvent = (IdleStateEvent) evt ; 27 IdleStateEvent idleStateEvent = (IdleStateEvent) evt ;
34 28
35 if (idleStateEvent.state() == IdleState.WRITER_IDLE){ 29 if (idleStateEvent.state() == IdleState.WRITER_IDLE){
@@ -40,7 +34,7 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> { @@ -40,7 +34,7 @@ public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf> {
40 } 34 }
41 35
42 36
43 - } 37 + }*/
44 38
45 super.userEventTriggered(ctx, evt); 39 super.userEventTriggered(ctx, evt);
46 } 40 }
@@ -11,6 +11,7 @@ netty.server.port=11211 @@ -11,6 +11,7 @@ netty.server.port=11211
11 11
12 logging.level.root=info 12 logging.level.root=info
13 13
  14 +# 通道 ID
14 channel.id=100 15 channel.id=100
15 16
16 17
1 package com.crossoverjie.netty.action.handle; 1 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.util.RandomUtil;  
5 import com.crossoverjie.netty.action.util.NettySocketHolder; 4 import com.crossoverjie.netty.action.util.NettySocketHolder;
6 import io.netty.buffer.ByteBuf; 5 import io.netty.buffer.ByteBuf;
7 import io.netty.buffer.Unpooled; 6 import io.netty.buffer.Unpooled;
8 -import io.netty.channel.ChannelFutureListener;  
9 import io.netty.channel.ChannelHandlerContext; 7 import io.netty.channel.ChannelHandlerContext;
10 import io.netty.channel.SimpleChannelInboundHandler; 8 import io.netty.channel.SimpleChannelInboundHandler;
11 -import io.netty.channel.socket.SocketChannel;  
12 -import io.netty.channel.socket.nio.NioServerSocketChannel;  
13 import io.netty.channel.socket.nio.NioSocketChannel; 9 import io.netty.channel.socket.nio.NioSocketChannel;
14 -import io.netty.handler.timeout.IdleState;  
15 -import io.netty.handler.timeout.IdleStateEvent;  
16 import io.netty.util.CharsetUtil; 10 import io.netty.util.CharsetUtil;
17 import org.slf4j.Logger; 11 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
@@ -24,7 +18,7 @@ import org.slf4j.LoggerFactory; @@ -24,7 +18,7 @@ import org.slf4j.LoggerFactory;
24 * Date: 17/05/2018 18:52 18 * Date: 17/05/2018 18:52
25 * @since JDK 1.8 19 * @since JDK 1.8
26 */ 20 */
27 -public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomProtocol> { 21 +public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<String> {
28 22
29 private final static Logger LOGGER = LoggerFactory.getLogger(HeartBeatSimpleHandle.class); 23 private final static Logger LOGGER = LoggerFactory.getLogger(HeartBeatSimpleHandle.class);
30 24
@@ -45,7 +39,7 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro @@ -45,7 +39,7 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro
45 @Override 39 @Override
46 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { 40 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
47 41
48 - if (evt instanceof IdleStateEvent){ 42 + /*if (evt instanceof IdleStateEvent){
49 IdleStateEvent idleStateEvent = (IdleStateEvent) evt ; 43 IdleStateEvent idleStateEvent = (IdleStateEvent) evt ;
50 44
51 if (idleStateEvent.state() == IdleState.READER_IDLE){ 45 if (idleStateEvent.state() == IdleState.READER_IDLE){
@@ -55,16 +49,16 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro @@ -55,16 +49,16 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro
55 } 49 }
56 50
57 51
58 - } 52 + }*/
59 53
60 super.userEventTriggered(ctx, evt); 54 super.userEventTriggered(ctx, evt);
61 } 55 }
62 56
63 @Override 57 @Override
64 - protected void channelRead0(ChannelHandlerContext ctx, CustomProtocol customProtocol) throws Exception {  
65 - LOGGER.info("收到customProtocol={}", customProtocol); 58 + protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
  59 + LOGGER.info("收到msg={}", msg);
66 60
67 //保存客户端与 Channel 之间的关系 61 //保存客户端与 Channel 之间的关系
68 - NettySocketHolder.put(customProtocol.getId(),(NioSocketChannel)ctx.channel()) ; 62 + //NettySocketHolder.put(CustomProtocolProtocol.getId(),(NioSocketChannel)ctx.channel()) ;
69 } 63 }
70 } 64 }
1 package com.crossoverjie.netty.action.init; 1 package com.crossoverjie.netty.action.init;
2 2
3 import com.crossoverjie.netty.action.handle.HeartBeatSimpleHandle; 3 import com.crossoverjie.netty.action.handle.HeartBeatSimpleHandle;
4 -import com.crossoverjie.netty.action.decoder.HeartbeatDecoder;  
5 import io.netty.channel.Channel; 4 import io.netty.channel.Channel;
6 import io.netty.channel.ChannelInitializer; 5 import io.netty.channel.ChannelInitializer;
  6 +import io.netty.handler.codec.LineBasedFrameDecoder;
  7 +import io.netty.handler.codec.string.StringDecoder;
7 import io.netty.handler.timeout.IdleStateHandler; 8 import io.netty.handler.timeout.IdleStateHandler;
8 9
9 /** 10 /**
@@ -19,7 +20,9 @@ public class HeartbeatInitializer extends ChannelInitializer<Channel> { @@ -19,7 +20,9 @@ public class HeartbeatInitializer extends ChannelInitializer<Channel> {
19 ch.pipeline() 20 ch.pipeline()
20 //五秒没有收到消息 将IdleStateHandler 添加到 ChannelPipeline 中 21 //五秒没有收到消息 将IdleStateHandler 添加到 ChannelPipeline 中
21 .addLast(new IdleStateHandler(5, 0, 0)) 22 .addLast(new IdleStateHandler(5, 0, 0))
22 - .addLast(new HeartbeatDecoder()) 23 + //.addLast(new HeartbeatDecoder())
  24 + .addLast(new LineBasedFrameDecoder(1024))
  25 + .addLast(new StringDecoder())
23 .addLast(new HeartBeatSimpleHandle()); 26 .addLast(new HeartBeatSimpleHandle());
24 } 27 }
25 } 28 }