Skip to content

Getting started

Long Tran edited this page Mar 13, 2018 · 1 revision

First steps

Simply create an instance of SteamClient and then call connect() to start connecting to the steam servers.

SteamClient client = new SteamClient();
client.connect();

You can customize the configuration of the client (e.g. to specify a protocol or a ServerListProvider).

SteamConfiguration configuration = SteamConfiguration.create(builder -> {
    builder.setProtocolTypes(ProtocolTypes.TCP);
    builder.withServerListProvider(new FileServerListProvider(new File("servers.bin")));
});

SteamClient client = new SteamClient(configuration);

Callbacks

To receive callbacks from the client when there is an incoming message you have to register callback consumers with a CallbackManager object. The following code registers callbacks for connection and disconnection events.

CallbackManager manager = new CallbackManager(steamClient);
manager.subscribe(ConnectedCallback.class, this::onConnected);
manager.subscribe(DisconnectedCallback.class, this::onDisconnected);

...

private void onConnected(LoggedOnCallback callback) {
    // handle connected event here, e.g. log in
}

private void onDisconnected(DisconnectedCallback callback) {
    // handle disconnected event here, e.g. reconnect
}

To actually start receiving the callbacks you need to call one of the run methods. Normally you would do this in a separate thread in a loop.

while (isRunning) {
    manager.runWaitCallbacks(1000L);
}

Logging

By default log messages created by JavaSteam are not redirected anywhere. You can use the DefaultLogListener which simply prints log messages to the standard output. Alternatively, you can implement the LogListener interface if you want to handle the log messages yourself.

LogManager.addListener(new DefaultLogListener());