2017-08-08 7 views
0

Je souhaite envoyer l'état mondial toutes les 100 ms à tous les canaux. Mais il n'appelle qu'une seule fois.Boucle logique de jeu de base pour envoyer des données aux canaux dans Netty

Mon code:

 public class IncomeMessageTcpHandler extends SimpleChannelInboundHandler<byte[]> { 

@Override 
    public void channelActive(ChannelHandlerContext ctx) throws Exception { 
     Channel channel = ctx.channel(); 

     channel.eventLoop().scheduleAtFixedRate(new Runnable() { 
      @Override 
      public void run() { 
       System.out.println("send"); 
       channel.writeAndFlush(GameLogic.Instance.flushMessageData()); 
      } 
     }, 100, 100, TimeUnit.MILLISECONDS); 
    } 
} 

Maintenant, l'appel de méthode une seule fois. J'utilise 4.1.13.Final

public class UnityServerTcpChannelInitializer extends ChannelInitializer<SocketChannel> { 
    @Override 
    public void initChannel(SocketChannel ch) throws Exception { 
     ChannelPipeline p = ch.pipeline(); 

     p.addLast("frameDecoder", new FixedLengthFrameDecoder(6)); 
     p.addLast("bytesDecoder", new ByteArrayDecoder()); 

     p.addLast("bytesEncoder", new ByteArrayEncoder()); 

     p.addLast(new IncomeMessageTcpHandler()); 
    } 
} 

Quand je commente channel.writeAndFlush (GameLogic.Instance.flushMessageData()); tâche exécutée toutes les 100 msec

Répondre

1

Omg ... ma faute. Ma méthode GameLogic.Instance.flushMessageData() génère NullPointerException mais je ne savais pas que Runnable ne lance aucune exception. C'est pourquoi il arrête de fonctionner sans prévenir