2017-10-11 6 views
0

Spigot Server에서 외부 Netty Server를 시작하려고했습니다.Spigot Server에서 외부 Netty Server를 시작하는 방법

제가 시도한 유일한 문제는 처음부터 시작했지만 사용자가 참여할 수없고 서버 시간 초과 문제가 발생했습니다.

이것은 잘 작동하는 Netty-Server에 연결해야하는 Netty-Client의 코드입니다. 이 closeFuture().sync();를 사용하여 종료하는 코드와

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()를 사용하여 서버를 시작, 당신은 기다립니다.

연결이 끝날 때까지 기다리고 있기 때문에 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(); 

}