如何在Spigot服务器上启动外部Netty Server

时间:2017-10-11 19:31:17

标签: java netty bukkit

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

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

这是Netty-Client的代码,应该连接到运行良好的Netty-Server。

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

1 个答案:

答案 0 :(得分:2)

使用您的代码,您使用.connect().sync()启动服务器,然后等待它使用closeFuture().sync();退出。

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

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

您可以在插件中执行的操作是在onEnable中创建一个新的eventLoopGroup,然后在以后创建一个新的网络连接,当您插件被禁用时,请断开连接。

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

}