|
@@ -1,9 +1,6 @@
|
|
|
package com.dzdy.dim.client;
|
|
|
|
|
|
-import com.dzdy.dim.client.handler.HeartBeatTimerHandler;
|
|
|
-import com.dzdy.dim.client.handler.LoginResponseHandler;
|
|
|
-import com.dzdy.dim.client.handler.LogoutResponseHandler;
|
|
|
-import com.dzdy.dim.client.handler.SockDataRequestHandler;
|
|
|
+import com.dzdy.dim.client.handler.*;
|
|
|
import com.dzdy.dim.handler.IMIdleStateHandler;
|
|
|
import com.dzdy.dim.handler.PacketDecoder;
|
|
|
import com.dzdy.dim.handler.PacketEncoder;
|
|
@@ -29,18 +26,20 @@ public class NettyClient {
|
|
|
/**
|
|
|
* 最大连接重试次数
|
|
|
*/
|
|
|
- private static final int MAX_RETRY = 5;
|
|
|
+ public static final int MAX_RETRY = 5;
|
|
|
+ private static final int PORT = 9002;
|
|
|
+ private static final String HOST = "127.0.0.1";
|
|
|
+ private static Bootstrap BOOTSTRAP = new Bootstrap();
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
connect();
|
|
|
}
|
|
|
|
|
|
private static void connect() {
|
|
|
- Bootstrap bootstrap = new Bootstrap();
|
|
|
|
|
|
NioEventLoopGroup group = new NioEventLoopGroup();
|
|
|
|
|
|
- bootstrap.group(group).channel(NioSocketChannel.class)
|
|
|
+ BOOTSTRAP.group(group).channel(NioSocketChannel.class)
|
|
|
// I/O 处理逻辑
|
|
|
.handler(new ChannelInitializer<Channel>() {
|
|
|
@Override
|
|
@@ -48,6 +47,8 @@ public class NettyClient {
|
|
|
ch.pipeline().addLast(new IMIdleStateHandler());
|
|
|
ch.pipeline().addLast(new Spliter());
|
|
|
ch.pipeline().addLast(new PacketDecoder());
|
|
|
+ // 心跳检测响应
|
|
|
+ ch.pipeline().addLast(new HeartBeatResponseHandler());
|
|
|
// 登录响应
|
|
|
ch.pipeline().addLast(new LoginResponseHandler());
|
|
|
// 登出响应
|
|
@@ -59,10 +60,10 @@ public class NettyClient {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- connect(bootstrap, "127.0.0.1", 9002, MAX_RETRY);
|
|
|
+ connect(MAX_RETRY);
|
|
|
|
|
|
//连接超时
|
|
|
- bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
|
|
|
+ BOOTSTRAP.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
|
|
|
.option(ChannelOption.SO_KEEPALIVE, true)
|
|
|
.option(ChannelOption.TCP_NODELAY, true);
|
|
|
|
|
@@ -75,7 +76,7 @@ public class NettyClient {
|
|
|
*/
|
|
|
private static void login(Channel channel) {
|
|
|
LoginRequestPacket loginRequestPacket = new LoginRequestPacket();
|
|
|
- loginRequestPacket.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTk1NDY5MDMsInVzZXJfbmFtZSI6ImpnemgwMSIsImF1dGhvcml0aWVzIjpbIlJPTEVfT1BFUkFUT1IiXSwianRpIjoiZjA2NmQxMGMtZWVmMy00Mzk4LTg0NjctOTNiNGYwNWRiMTEwIiwiY2xpZW50X2lkIjoiYW5waW4tb3BzLXBjIiwic2NvcGUiOlsic2VsZWN0Il19.jKTFpZitBmfnLsjssOa5EBNmSwjYu90AgcfDy35fqN4");
|
|
|
+ loginRequestPacket.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjI1NTk1NDY5MDMsInVzZXJfbmFtZSI6ImpnemgwMSIsImF1dGhvcml0aWVzIjpbIlJPTEVfT1BFUkFUT1IiXSwianRpIjoiZjA2NmQxMGMtZWVmMy00Mzk4LTg0NjctOTNiNGYwNWRiMTEwIiwiY2xpZW50X2lkIjoiYW5waW4tb3BzLXBjIiwic2NvcGUiOlsic2VsZWN0Il19.yB-K7s25d9cVUCT4Ec4ag5T4uJSa0X2hWNM5UJdinLk");
|
|
|
channel.writeAndFlush(loginRequestPacket);
|
|
|
}
|
|
|
|
|
@@ -83,13 +84,10 @@ public class NettyClient {
|
|
|
/**
|
|
|
* 连接重试
|
|
|
*
|
|
|
- * @param bootstrap
|
|
|
- * @param host
|
|
|
- * @param port
|
|
|
* @param retry
|
|
|
*/
|
|
|
- private static void connect(Bootstrap bootstrap, String host, int port, int retry) {
|
|
|
- bootstrap.connect(host, port).addListener(future -> {
|
|
|
+ public static void connect(int retry) {
|
|
|
+ BOOTSTRAP.connect(HOST, PORT).addListener(future -> {
|
|
|
if (future.isSuccess()) {
|
|
|
Channel channel = ((ChannelFuture) future).channel();
|
|
|
login(channel);
|
|
@@ -101,7 +99,7 @@ public class NettyClient {
|
|
|
// 本次重连的间隔
|
|
|
int delay = 1 << order;
|
|
|
System.err.println(System.currentTimeMillis() + ": 连接失败,第" + order + "次重连……");
|
|
|
- bootstrap.config().group().schedule(() -> connect(bootstrap, host, port, retry - 1), delay, TimeUnit
|
|
|
+ BOOTSTRAP.config().group().schedule(() -> connect(retry - 1), delay, TimeUnit
|
|
|
.SECONDS);
|
|
|
}
|
|
|
});
|