0

我使用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..
  }
}
4

3 回答 3

0

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

new Thread(client).start();
new Thread(server).start();
于 2013-11-26T17:11:20.817 回答
0

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

于 2013-09-16T09:58:30.217 回答
-1

我从未使用过 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"));
        } 

      }
    }
于 2013-09-16T09:51:30.570 回答