Longi94 / JavaSteam

Java library that provides an interface to directly interact with Valve's Steam servers.
MIT License
96 stars 20 forks source link

Callbacks Not Consistently Triggering in JavaSteam Client on Separate Thread with SpringBoot Application #292

Closed guilhermec-costa closed 1 month ago

guilhermec-costa commented 1 month ago

Hi, I’m integrating the JavaSteam library (Longi94) into a Spring Boot application and facing issues with callbacks not being consistently triggered after connecting to the Steam client.

Here’s what’s happening:

I’m using the SteamClient, and the logs confirm a successful connection to Steam servers (e.g., "Connected to /155.133.227.34:27034"). However, the onConnected callback (subscribed via CallbackManager) is intermittently triggered. Sometimes it works, but other times it doesn’t, despite no code changes. I’m running cbManager.runWaitCallbacks() continuously in a separate thread, and I suspect this might be related to the issue. I’m not sure if this is a bug in the library or a mistake in my implementation. Could the issue be due to threading, or is there something wrong with my setup?

image

Here's the entry point of my application:

  private Thread webApiThread;

  public static void main(String[] args) {
    SpringApplication.run(AnalyticsApplication.class, args);
  }

  @EventListener(classes = ApplicationReadyEvent.class)
  public void startWebApiRunnable() {
    LogManager.addListener(new DefaultLogListener());
    webApiThread = new Thread(new WebApiRunnable());
    webApiThread.start();
  }
}

And this is the implementation of the "run" method, that gets executed when the spring application finishes starting:

public class WebApiRunnable implements Runnable {

  private SteamClient steamClient;
  private boolean isExecuting;
  private final String username;
  private final String password;
  private SteamUser steamUser;
  private CallbackManager cbManager;
  private Scanner scanner;

  public WebApiRunnable() {
    this.username = "some_env_variable";
    this.password = "some_env_variable";
    this.scanner = new Scanner(System.in);
  }

  @Override
  public void run() {
    SteamConfiguration configuration = SteamConfiguration.create(builder -> {
      builder.withProtocolTypes(ProtocolTypes.TCP);
    });

    steamClient = new SteamClient(configuration);
    cbManager = new CallbackManager(steamClient);
    steamUser = steamClient.getHandler(SteamUser.class);

    // Subscribing callbacks
    cbManager.subscribe(ConnectedCallback.class, this::onConnected);
    cbManager.subscribe(DisconnectedCallback.class, this::onDisconnected);
    cbManager.subscribe(LoggedOnCallback.class, this::onLoggedOn);
    cbManager.subscribe(LoggedOffCallback.class, this::onLoggedOff);

    isExecuting = true;
    steamClient.connect();

    // Main callback loop
    while (isExecuting) {
      cbManager.runWaitCallbacks(1000L);
    }
  }

  // Callback implementations
  private void onConnected(ConnectedCallback callback) {
    System.out.println("Connected to Steam!");
    // Additional logic
  }

  // More callback implementations here...
}
LossyDragon commented 1 month ago

Valve broke CM's last week, starting Friday (October 11th) morning and mostly** fixed it sometime Monday. TCP connections has been weird since this. (They also seriously broke WebSockets with most of the CM's only supporing TLS 1.3, breaking their iOS apps too).

You can try setting your withProtocolTypes to WEB_SOCKET and try connecting.

Otherwise, feel free to try 1.6.0-SNAPSHOT which uses GetCMListForConnect which seems to prefer websockets more than TCP/UDP. Also this snapshot allows either WEBSOCKET or TCP now as default connections.

guilhermec-costa commented 1 month ago

Really appreciate your help @LossyDragon. Thank you for the explanation about CM's. It did work now! I switched the protocol type to WEB_SOCKET. And I was following the sample called "SampleSteamGuardRememberMe", which I noticied it is considered legacy at this point. So I followed the "SampleLogonAuthentication" and everything worked well. Also, thank you for the promptitude of the response!