Introducing new features.客户端 push 消息 OK
正在显示
10 个修改的文件
包含
29 行增加
和
16 行删除
| @@ -2,8 +2,8 @@ package com.crossoverjie.netty.action.common.res; | @@ -2,8 +2,8 @@ package com.crossoverjie.netty.action.common.res; | ||
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | -import com.crossoverjie.netty.action.common.enums.StatusEnum; | ||
| 6 | import com.crossoverjie.netty.action.common.util.StringUtil; | 5 | import com.crossoverjie.netty.action.common.util.StringUtil; |
| 6 | +import com.crossoverjie.netty.action.common.enums.StatusEnum; | ||
| 7 | 7 | ||
| 8 | import java.io.Serializable; | 8 | import java.io.Serializable; |
| 9 | 9 |
| 1 | package com.crossoverjie.netty.action.client; | 1 | package com.crossoverjie.netty.action.client; |
| 2 | 2 | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 3 | import com.crossoverjie.netty.action.client.init.CustomerHandleInitializer; | 4 | import com.crossoverjie.netty.action.client.init.CustomerHandleInitializer; |
| 4 | import com.crossoverjie.netty.action.common.pojo.CustomProtocol; | 5 | import com.crossoverjie.netty.action.common.pojo.CustomProtocol; |
| 5 | import io.netty.bootstrap.Bootstrap; | 6 | import io.netty.bootstrap.Bootstrap; |
| 6 | import io.netty.channel.ChannelFuture; | 7 | import io.netty.channel.ChannelFuture; |
| 8 | +import io.netty.channel.ChannelFutureListener; | ||
| 7 | import io.netty.channel.EventLoopGroup; | 9 | import io.netty.channel.EventLoopGroup; |
| 8 | import io.netty.channel.nio.NioEventLoopGroup; | 10 | import io.netty.channel.nio.NioEventLoopGroup; |
| 9 | import io.netty.channel.socket.SocketChannel; | 11 | import io.netty.channel.socket.SocketChannel; |
| @@ -58,6 +60,9 @@ public class HeartbeatClient { | @@ -58,6 +60,9 @@ public class HeartbeatClient { | ||
| 58 | * @param customProtocol | 60 | * @param customProtocol |
| 59 | */ | 61 | */ |
| 60 | public void sendMsg(CustomProtocol customProtocol){ | 62 | public void sendMsg(CustomProtocol customProtocol){ |
| 61 | - channel.writeAndFlush(customProtocol) ; | 63 | + ChannelFuture future = channel.writeAndFlush(customProtocol); |
| 64 | + future.addListener((ChannelFutureListener) channelFuture -> | ||
| 65 | + LOGGER.info("手动发消息成功={}", JSON.toJSONString(customProtocol))); | ||
| 66 | + | ||
| 62 | } | 67 | } |
| 63 | } | 68 | } |
| 1 | package com.crossoverjie.netty.action.client; | 1 | package com.crossoverjie.netty.action.client; |
| 2 | 2 | ||
| 3 | -import com.crossoverjie.netty.action.common.pojo.CustomProtocol; | ||
| 4 | import org.slf4j.Logger; | 3 | import org.slf4j.Logger; |
| 5 | import org.slf4j.LoggerFactory; | 4 | import org.slf4j.LoggerFactory; |
| 6 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 7 | import org.springframework.boot.CommandLineRunner; | 5 | import org.springframework.boot.CommandLineRunner; |
| 8 | import org.springframework.boot.SpringApplication; | 6 | import org.springframework.boot.SpringApplication; |
| 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; | 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 1 | -package com.crossoverjie.netty.action.client.channel.init; | 1 | +package com.crossoverjie.netty.action.channel.init; |
| 2 | 2 | ||
| 3 | -import com.crossoverjie.netty.action.client.handle.HeartBeatSimpleHandle; | ||
| 4 | -import com.crossoverjie.netty.action.client.decoder.HeartbeatDecoder; | 3 | +import com.crossoverjie.netty.action.handle.HeartBeatSimpleHandle; |
| 4 | +import com.crossoverjie.netty.action.decoder.HeartbeatDecoder; | ||
| 5 | import io.netty.channel.Channel; | 5 | import io.netty.channel.Channel; |
| 6 | import io.netty.channel.ChannelInitializer; | 6 | import io.netty.channel.ChannelInitializer; |
| 7 | import io.netty.handler.timeout.IdleStateHandler; | 7 | import io.netty.handler.timeout.IdleStateHandler; |
| 1 | -package com.crossoverjie.netty.action.client.decoder; | 1 | +package com.crossoverjie.netty.action.decoder; |
| 2 | 2 | ||
| 3 | import com.crossoverjie.netty.action.common.pojo.CustomProtocol; | 3 | import com.crossoverjie.netty.action.common.pojo.CustomProtocol; |
| 4 | import io.netty.buffer.ByteBuf; | 4 | import io.netty.buffer.ByteBuf; |
| 1 | -package com.crossoverjie.netty.action.client.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 io.netty.buffer.Unpooled; | 4 | import io.netty.buffer.Unpooled; |
| 1 | -package com.crossoverjie.netty.action.client.server; | 1 | +package com.crossoverjie.netty.action.server; |
| 2 | 2 | ||
| 3 | -import com.crossoverjie.netty.action.client.channel.init.HeartbeatInitializer; | 3 | +import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer; |
| 4 | +import com.crossoverjie.netty.action.common.pojo.CustomProtocol; | ||
| 4 | import io.netty.bootstrap.ServerBootstrap; | 5 | import io.netty.bootstrap.ServerBootstrap; |
| 5 | import io.netty.channel.*; | 6 | import io.netty.channel.*; |
| 6 | import io.netty.channel.nio.NioEventLoopGroup; | 7 | import io.netty.channel.nio.NioEventLoopGroup; |
| @@ -34,7 +35,7 @@ public class HeartBeatServer { | @@ -34,7 +35,7 @@ public class HeartBeatServer { | ||
| 34 | @Value("${netty.server.port}") | 35 | @Value("${netty.server.port}") |
| 35 | private int nettyPort ; | 36 | private int nettyPort ; |
| 36 | 37 | ||
| 37 | - private SocketChannel channel ; | 38 | + private NioServerSocketChannel channel ; |
| 38 | 39 | ||
| 39 | /** | 40 | /** |
| 40 | * 启动 Netty | 41 | * 启动 Netty |
| @@ -56,7 +57,7 @@ public class HeartBeatServer { | @@ -56,7 +57,7 @@ public class HeartBeatServer { | ||
| 56 | if (future.isSuccess()){ | 57 | if (future.isSuccess()){ |
| 57 | LOGGER.info("启动 Netty 成功"); | 58 | LOGGER.info("启动 Netty 成功"); |
| 58 | } | 59 | } |
| 59 | - channel = (SocketChannel) future.channel() ; | 60 | + channel = (NioServerSocketChannel) future.channel() ; |
| 60 | } | 61 | } |
| 61 | 62 | ||
| 62 | 63 | ||
| @@ -69,4 +70,13 @@ public class HeartBeatServer { | @@ -69,4 +70,13 @@ public class HeartBeatServer { | ||
| 69 | work.shutdownGracefully().syncUninterruptibly() ; | 70 | work.shutdownGracefully().syncUninterruptibly() ; |
| 70 | LOGGER.info("关闭 Netty 成功"); | 71 | LOGGER.info("关闭 Netty 成功"); |
| 71 | } | 72 | } |
| 73 | + | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * 发送消息 | ||
| 77 | + * @param customProtocol | ||
| 78 | + */ | ||
| 79 | + public void sendMsg(CustomProtocol customProtocol){ | ||
| 80 | + channel.writeAndFlush(customProtocol) ; | ||
| 81 | + } | ||
| 72 | } | 82 | } |
-
请 注册 或 登录 后发表评论