rybalkinsd / atom

Java course materials

Home Page:https://www.youtube.com/playlist?list=PLrCZzMib1e9pnFbVV3u4s7ki5NTnm7WgT

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

В клиенте session null

patakiph opened this issue · comments

В классе ServerConnectionSocket

 @OnWebSocketConnect
  public void onConnect(@NotNull Session session) throws IOException {
    this.session = session;

    log.info("Connected!");

    new PacketAuth(Game.login, Game.serverToken).write();
  }

@NotNull Session session иногда является null, поэтому клиент не конектится к серверу. В отладке всё ок. Connection создаётся в конструкторе класса Game()

public Game() {
    this.gameServerUrl = "ws://" + (JOptionPane.showInputDialog(null, "Host", DEFAULT_GAME_SERVER_HOST + ":" + DEFAULT_GAME_SERVER_PORT));

    authenticate();

    final WebSocketClient client = new WebSocketClient();
    socket = new ServerConnectionSocket();
    new Thread(() -> {
      try {
        client.start();
        URI serverURI = new URI(gameServerUrl + "/clientConnection");
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        request.setHeader("Origin", "zagar.io");
        client.connect(socket, serverURI, request); /// ->ПОХОДУ ТУТ ЧТО-ТО не так
        log.info("Trying to connect <" + gameServerUrl + ">");
        socket.awaitClose(7, TimeUnit.DAYS);
      } catch (Throwable t) {
        t.printStackTrace();
      }
    }).start();
  }