mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-07 22:07:55 +08:00
fix 服务端添加心跳监听
This commit is contained in:
parent
4f8d1d206c
commit
cbd0d7c954
@ -24,7 +24,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>top.wu2020</groupId>
|
<groupId>top.wu2020</groupId>
|
||||||
<artifactId>wu-framework-web</artifactId>
|
<artifactId>wu-framework-web</artifactId>
|
||||||
<version>1.1.9-JDK1.8-SNAPSHOT</version>
|
<version>1.1.9-JDK1.8</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -70,53 +70,50 @@ public class ProxySocket {
|
|||||||
});
|
});
|
||||||
|
|
||||||
log.info("连接服务端IP:{},连接服务端端口:{}",Constant.serverIp, Constant.serverPort);
|
log.info("连接服务端IP:{},连接服务端端口:{}",Constant.serverIp, Constant.serverPort);
|
||||||
bootstrap.connect(Constant.serverIp, Constant.serverPort).addListener(new ChannelFutureListener() {
|
bootstrap.connect(Constant.serverIp, Constant.serverPort).addListener((ChannelFutureListener) future -> {
|
||||||
@Override
|
if (future.isSuccess()) {
|
||||||
public void operationComplete(ChannelFuture future) throws Exception {
|
System.out.println("连接服务端成功");
|
||||||
if (future.isSuccess()) {
|
// 客户端链接代理服务器成功
|
||||||
System.out.println("连接服务端成功");
|
Channel channel = future.channel();
|
||||||
// 客户端链接代理服务器成功
|
if (StringUtil.isNullOrEmpty(vid)) {
|
||||||
Channel channel = future.channel();
|
// 告诉服务端这条连接是client的连接
|
||||||
if (StringUtil.isNullOrEmpty(vid)) {
|
MyMsg myMsg = new MyMsg();
|
||||||
// 告诉服务端这条连接是client的连接
|
myMsg.setType(MyMsg.TYPE_CONNECT);
|
||||||
MyMsg myMsg = new MyMsg();
|
myMsg.setData("client".getBytes());
|
||||||
myMsg.setType(MyMsg.TYPE_CONNECT);
|
channel.writeAndFlush(myMsg);
|
||||||
myMsg.setData("client".getBytes());
|
|
||||||
channel.writeAndFlush(myMsg);
|
|
||||||
|
|
||||||
Constant.proxyChannel = channel;
|
Constant.proxyChannel = channel;
|
||||||
} else {
|
|
||||||
|
|
||||||
// 告诉服务端这条连接是vid的连接
|
|
||||||
MyMsg myMsg = new MyMsg();
|
|
||||||
myMsg.setType(MyMsg.TYPE_CONNECT);
|
|
||||||
myMsg.setData(vid.getBytes());
|
|
||||||
channel.writeAndFlush(myMsg);
|
|
||||||
|
|
||||||
// 客户端绑定通道关系
|
|
||||||
Constant.vpc.put(vid, channel);
|
|
||||||
channel.attr(Constant.VID).set(vid);
|
|
||||||
|
|
||||||
Channel realChannel = Constant.vrc.get(vid);
|
|
||||||
if (null != realChannel) {
|
|
||||||
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
System.out.println("每隔2s重连....");
|
// 告诉服务端这条连接是vid的连接
|
||||||
future.channel().eventLoop().schedule(new Runnable() {
|
MyMsg myMsg = new MyMsg();
|
||||||
|
myMsg.setType(MyMsg.TYPE_CONNECT);
|
||||||
|
myMsg.setData(vid.getBytes());
|
||||||
|
channel.writeAndFlush(myMsg);
|
||||||
|
|
||||||
@Override
|
// 客户端绑定通道关系
|
||||||
public void run() {
|
Constant.vpc.put(vid, channel);
|
||||||
try {
|
channel.attr(Constant.VID).set(vid);
|
||||||
newConnect(null);
|
|
||||||
} catch (InterruptedException e) {
|
Channel realChannel = Constant.vrc.get(vid);
|
||||||
e.printStackTrace();
|
if (null != realChannel) {
|
||||||
}
|
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||||
}
|
}
|
||||||
}, 2, TimeUnit.SECONDS);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
System.out.println("每隔2s重连....");
|
||||||
|
future.channel().eventLoop().schedule(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
newConnect(null);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 2, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,9 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
|||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import io.netty.util.internal.StringUtil;
|
import io.netty.util.internal.StringUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class RealSocket {
|
public class RealSocket {
|
||||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
@ -41,6 +43,7 @@ public class RealSocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
log.info("代理客户端本地端口:{}",Constant.realPort);
|
||||||
bootstrap.connect("127.0.0.1", Constant.realPort).addListener(new ChannelFutureListener() {
|
bootstrap.connect("127.0.0.1", Constant.realPort).addListener(new ChannelFutureListener() {
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(ChannelFuture future) throws Exception {
|
public void operationComplete(ChannelFuture future) throws Exception {
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>top.wu2020</groupId>
|
<groupId>top.wu2020</groupId>
|
||||||
<artifactId>wu-framework-web</artifactId>
|
<artifactId>wu-framework-web</artifactId>
|
||||||
<version>1.1.9-JDK1.8-SNAPSHOT</version>
|
<version>1.1.9-JDK1.8</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
Loading…
x
Reference in New Issue
Block a user