2017-10-11 176 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(); 

}