Java 如何在插口服务器上启动外部Netty服务器
我试图在插口服务器上启动外部Netty服务器 我唯一尝试的是在一开始就启动它,但问题是用户无法加入,服务器超时 这是Netty客户端的代码,它应该连接到工作正常的Netty服务器Java 如何在插口服务器上启动外部Netty服务器,java,netty,bukkit,Java,Netty,Bukkit,我试图在插口服务器上启动外部Netty服务器 我唯一尝试的是在一开始就启动它,但问题是用户无法加入,服务器超时 这是Netty客户端的代码,它应该连接到工作正常的Netty服务器 EventLoopGroup eventLoopGroup = EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap() .group( eve
EventLoopGroup eventLoopGroup = EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group( eventLoopGroup )
.option( ChannelOption.TCP_NODELAY, true )
.option( ChannelOption.SO_KEEPALIVE, true )
.channel( EPOLL ? EpollSocketChannel.class : NioSocketChannel.class )
.handler( new ChannelInitializer<Channel>() {
protected void initChannel( Channel channel ) throws Exception {
preparePipeline( channel );
}
} );
ChannelFuture f = bootstrap.connect(
ReplaySpigotServer.getConnection().configuration.getString( "server-host" ),
ReplaySpigotServer.getConnection().configuration.getInt( "server-port" ) )
.sync();
f.channel().closeFuture().sync();
} catch ( InterruptedException e ) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
EventLoopGroup EventLoopGroup=EPOLL?新的EPolleEventLoopGroup():新的NioEventLoopGroup();
试一试{
Bootstrap Bootstrap=new Bootstrap()
.group(eventLoopGroup)
.option(ChannelOption.TCP_NODELAY,true)
.option(ChannelOption.SO_KEEPALIVE,true)
.channel(EPOLL?EpollSocketChannel.class:NioSocketChannel.class)
.handler(新的通道初始值设定项(){
受保护的无效初始化通道(通道通道)引发异常{
准备线(通道);
}
} );
ChannelFuture f=引导。连接(
ReplaySpigotServer.getConnection().configuration.getString(“服务器主机”),
ReplaySpigotServer.getConnection().configuration.getInt(“服务器端口”))
.sync();
f、 通道().closeFuture().sync();
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
eventLoopGroup.shutdownGracefully();
使用代码,您可以使用.connect().sync()
启动服务器,然后使用closeFuture().sync();
等待它退出
因为您正在等待连接结束,这意味着当您使用netty通道时,Bukkit/Spigot服务器无法处理任何与用户相关的数据包
由于调用eventLoopGroup.shutdownGracefully();
将意味着所有打开的连接都将关闭,因此我们需要使用某种方法来防止这种情况
你可以在插件中做的是在OneTable中创建一个新的eventLoopGroup
,然后创建一个新的netty连接,当你的插件被禁用时,断开连接
private EventLoopGroup eventLoopGroup;
public void onEnable(){
eventLoopGroup = EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup();
}
public void onDisable(){
eventLoopGroup.shutdownGracefully();
}
public void newConnection() {
Bootstrap bootstrap = new Bootstrap()
.group( eventLoopGroup )
.option( ChannelOption.TCP_NODELAY, true )
.option( ChannelOption.SO_KEEPALIVE, true )
.channel( EPOLL ? EpollSocketChannel.class : NioSocketChannel.class )
.handler( new ChannelInitializer<Channel>() {
protected void initChannel( Channel channel ) throws Exception {
preparePipeline( channel );
}
} );
ChannelFuture f = bootstrap.connect(
ReplaySpigotServer.getConnection().configuration.getString( "server-host" ),
ReplaySpigotServer.getConnection().configuration.getInt( "server-port" ) )
.sync();
}
private EventLoopGroup EventLoopGroup;
public void onEnable(){
eventLoopGroup=EPOLL?new-EpollEventLoopGroup():new-NioEventLoopGroup();
}
公共无效不可撤销(){
eventLoopGroup.shutdownGracefully();
}
公共连接(){
Bootstrap Bootstrap=new Bootstrap()
.group(eventLoopGroup)
.option(ChannelOption.TCP_NODELAY,true)
.option(ChannelOption.SO_KEEPALIVE,true)
.channel(EPOLL?EpollSocketChannel.class:NioSocketChannel.class)
.handler(新的通道初始值设定项(){
受保护的无效初始化通道(通道通道)引发异常{
准备线(通道);
}
} );
ChannelFuture f=引导。连接(
ReplaySpigotServer.getConnection().configuration.getString(“服务器主机”),
ReplaySpigotServer.getConnection().configuration.getInt(“服务器端口”))
.sync();
}
使用代码,您可以使用.connect().sync()
启动服务器,然后使用closeFuture().sync();
等待它退出
因为您正在等待连接结束,这意味着当您使用netty通道时,Bukkit/Spigot服务器无法处理任何与用户相关的数据包
由于调用eventLoopGroup.shutdownGracefully();
将意味着所有打开的连接都将关闭,因此我们需要使用某种方法来防止这种情况
你可以在插件中做的是在OneTable中创建一个新的eventLoopGroup
,然后创建一个新的netty连接,当你的插件被禁用时,断开连接
private EventLoopGroup eventLoopGroup;
public void onEnable(){
eventLoopGroup = EPOLL ? new EpollEventLoopGroup() : new NioEventLoopGroup();
}
public void onDisable(){
eventLoopGroup.shutdownGracefully();
}
public void newConnection() {
Bootstrap bootstrap = new Bootstrap()
.group( eventLoopGroup )
.option( ChannelOption.TCP_NODELAY, true )
.option( ChannelOption.SO_KEEPALIVE, true )
.channel( EPOLL ? EpollSocketChannel.class : NioSocketChannel.class )
.handler( new ChannelInitializer<Channel>() {
protected void initChannel( Channel channel ) throws Exception {
preparePipeline( channel );
}
} );
ChannelFuture f = bootstrap.connect(
ReplaySpigotServer.getConnection().configuration.getString( "server-host" ),
ReplaySpigotServer.getConnection().configuration.getInt( "server-port" ) )
.sync();
}
private EventLoopGroup EventLoopGroup;
public void onEnable(){
eventLoopGroup=EPOLL?new-EpollEventLoopGroup():new-NioEventLoopGroup();
}
公共无效不可撤销(){
eventLoopGroup.shutdownGracefully();
}
公共连接(){
Bootstrap Bootstrap=new Bootstrap()
.group(eventLoopGroup)
.option(ChannelOption.TCP_NODELAY,true)
.option(ChannelOption.SO_KEEPALIVE,true)
.channel(EPOLL?EpollSocketChannel.class:NioSocketChannel.class)
.handler(新的通道初始值设定项(){
受保护的无效初始化通道(通道通道)引发异常{
准备线(通道);
}
} );
ChannelFuture f=引导。连接(
ReplaySpigotServer.getConnection().configuration.getString(“服务器主机”),
ReplaySpigotServer.getConnection().configuration.getInt(“服务器端口”))
.sync();
}