2017-10-11 158 views
0

我试图在Spigot服务器上启动外部Netty服务器。如何在Spigot服务器上启动外部Netty服务器

我试过的唯一的事情是我在开始时就启动它,但问题是用户无法加入并且服务器超时。

这是Netty客户端的代码,它应该连接到运行良好的Netty服务器。

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(); 

回答

2

与您的代码,您开始使用.connect().sync()服务器,那你就等着它使用closeFuture().sync();退出。

因为您正在等待连接结束,所以这意味着当您使用netty通道时,Bukkit/Spigot服务器无法处理任何与用户相关的数据包。

由于调用eventLoopGroup.shutdownGracefully();意味着所有打开的连接都关闭,我们需要使用某种方法来防止这种情况。

你可以在你的插件里做什么,在onEnable里面新建一个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(); 

}