1 Star 2 Fork 56

bugstack虫洞栈/物联

forked from baolinking/物联网关 
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
ToMasterDesign.java 2.03 KB
Copy Edit Raw Blame History
baolinking authored 2019-09-17 13:51 +08:00 . 物联网采集系统源码提交
/**
*
*/
package connect;
import java.util.Date;
import javax.annotation.Resource;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import jedis.Jedisclusters;
import masterpro.MasterChannelInitializer;
import masterpro.MasterDisconnectListener;
/**
* @Description: client------>master
* @version: v1.0.0
* @author: wbl
* @date: 2019年8月30日 上午10:04:30
*/
public class ToMasterDesign implements Runnable{
private int masterid = 0;
private String ip = "";
private int port = 0;
@Resource
public Jedisclusters jedisCluster;
public ToMasterDesign(int masterid, String ip,int port){
this.masterid = masterid;
this.ip =ip;
this.port = port;
}
@Override
public void run() {
connect();
}
public void connect(){
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap gate = new Bootstrap();
gate.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new MasterChannelInitializer(masterid));
ChannelFuture channelFuture = gate.connect(ip, port).sync();
if(channelFuture.channel().isActive()){
sendMsg(channelFuture.channel());
}
channelFuture.addListener(new MasterDisconnectListener(masterid,ip,port));
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
public void sendMsg(Channel channel) {
String str = "gateway_Login";
System.out.println("Login: ["+new Date() + "] "+channel.remoteAddress().toString().substring(1));
channel.writeAndFlush(str);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/bugstack_cn/gateway.git
git@gitee.com:bugstack_cn/gateway.git
bugstack_cn
gateway
物联
master

Search