作者 crossoverJie

:sparkles: Introducing new features.客户端 push 消息 OK

正在显示 10 个修改的文件 包含 29 行增加16 行删除
... ... @@ -2,8 +2,8 @@ package com.crossoverjie.netty.action.common.res;
import com.crossoverjie.netty.action.common.enums.StatusEnum;
import com.crossoverjie.netty.action.common.util.StringUtil;
import com.crossoverjie.netty.action.common.enums.StatusEnum;
import java.io.Serializable;
... ...
package com.crossoverjie.netty.action.client;
import com.alibaba.fastjson.JSON;
import com.crossoverjie.netty.action.client.init.CustomerHandleInitializer;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
... ... @@ -58,6 +60,9 @@ public class HeartbeatClient {
* @param customProtocol
*/
public void sendMsg(CustomProtocol customProtocol){
channel.writeAndFlush(customProtocol) ;
ChannelFuture future = channel.writeAndFlush(customProtocol);
future.addListener((ChannelFutureListener) channelFuture ->
LOGGER.info("手动发消息成功={}", JSON.toJSONString(customProtocol)));
}
}
... ...
package com.crossoverjie.netty.action.client;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
... ...
... ... @@ -7,4 +7,4 @@ swagger.enable = true
netty.server.host=127.0.0.1
netty.server.port=11211
logging.level.root=debug
\ No newline at end of file
logging.level.root=info
\ No newline at end of file
... ...
package com.crossoverjie.netty.action.client.channel.init;
package com.crossoverjie.netty.action.channel.init;
import com.crossoverjie.netty.action.client.handle.HeartBeatSimpleHandle;
import com.crossoverjie.netty.action.client.decoder.HeartbeatDecoder;
import com.crossoverjie.netty.action.handle.HeartBeatSimpleHandle;
import com.crossoverjie.netty.action.decoder.HeartbeatDecoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.timeout.IdleStateHandler;
... ...
package com.crossoverjie.netty.action.client.decoder;
package com.crossoverjie.netty.action.decoder;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import io.netty.buffer.ByteBuf;
... ...
package com.crossoverjie.netty.action.client.handle;
package com.crossoverjie.netty.action.handle;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import io.netty.buffer.Unpooled;
... ...
package com.crossoverjie.netty.action.client.server;
package com.crossoverjie.netty.action.server;
import com.crossoverjie.netty.action.client.channel.init.HeartbeatInitializer;
import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer;
import com.crossoverjie.netty.action.common.pojo.CustomProtocol;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
... ... @@ -34,7 +35,7 @@ public class HeartBeatServer {
@Value("${netty.server.port}")
private int nettyPort ;
private SocketChannel channel ;
private NioServerSocketChannel channel ;
/**
* 启动 Netty
... ... @@ -56,7 +57,7 @@ public class HeartBeatServer {
if (future.isSuccess()){
LOGGER.info("启动 Netty 成功");
}
channel = (SocketChannel) future.channel() ;
channel = (NioServerSocketChannel) future.channel() ;
}
... ... @@ -69,4 +70,13 @@ public class HeartBeatServer {
work.shutdownGracefully().syncUninterruptibly() ;
LOGGER.info("关闭 Netty 成功");
}
/**
* 发送消息
* @param customProtocol
*/
public void sendMsg(CustomProtocol customProtocol){
channel.writeAndFlush(customProtocol) ;
}
}
... ...
... ... @@ -3,4 +3,4 @@ server.port=8081
netty.server.port=11211
logging.level.root=debug
\ No newline at end of file
logging.level.root=info
\ No newline at end of file
... ...