Kryo - 如何使服务器保持运行并等待请求

时间:2013-09-16 09:29:55

标签: java kryo

我使用Kryo-net发送和接收消息。在服务器端我打开新线程并设置服务器,问题是线程在代码结束时结束,所以没有真正的请求侦听器。

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class MessagingServer implements Runnable{
  private Server server;

  public void stop(){
    this.server.stop();
  }  

  public MessagingServer(){ 
    this.server = new Server();
  }

  @Override
  public void run() {
    try{
      if(server!=null){ stop(); }      

      this.server.start();
      this.server.bind(54555, 54777);

      Kryo kryo = this.server.getKryo();
      kryo.register(NewRequiredJobRequest.class);
      kryo.register(NewRequiredJobResponse.class);

      server.addListener(new Listener() {
         @Override
         public void received (Connection connection, Object object) {
            if (object instanceof HelloRequest) {
               HelloRequest request = (HelloRequest)object;
               System.out.println(request.text);

               HelloResponse response = new HelloResponse();
               response.text = "Thanks!";
               connection.sendTCP(response);
            }
         }
      });
    } catch (Exception e) {
      System.out.println("kryo server exception"));
    } 

    // once the code reach here the thread is ended..
  }
}

3 个答案:

答案 0 :(得分:0)

我从不使用kryo,但我认为这会有所帮助。

    import com.esotericsoftware.kryo.Kryo;
    import com.esotericsoftware.kryonet.Connection;
    import com.esotericsoftware.kryonet.Listener;
    import com.esotericsoftware.kryonet.Server;

    public class MessagingServer implements Runnable{
      private Server server;

      public void stop(){
        this.server.stop();
      }  

      public MessagingServer(){ 
        this.server = new Server();
      }

      @Override
      public void run() {
        try{
          if(server!=null){ stop(); }      

          this.server.start();
          this.server.bind(54555, 54777);

          while(true) {

           Kryo kryo = this.server.getKryo();
           kryo.register(NewRequiredJobRequest.class);
           kryo.register(NewRequiredJobResponse.class);

           server.addListener(new Listener() {
             @Override
             public void received (Connection connection, Object object) {
                if (object instanceof HelloRequest) {
                   HelloRequest request = (HelloRequest)object;
                   System.out.println(request.text);

                   HelloResponse response = new HelloResponse();
                   response.text = "Thanks!";
                   connection.sendTCP(response);
                }
             }
           });
         }
        } catch (Exception e) {
          System.out.println("kryo server exception"));
        } 

      }
    }

答案 1 :(得分:0)

服务器对象可能有某种必须在循环中运行的listenaccept方法。

答案 2 :(得分:0)

KryoNet Client #start和Server #start启动守护程序线程。如果您的应用中没有其他非守护程序线程:

new Thread(client).start();
new Thread(server).start();