Crates.io | renet_steam |
lib.rs | renet_steam |
version | 0.0.2 |
source | src |
created_at | 2024-02-22 00:35:29.72425 |
updated_at | 2024-07-20 17:33:37.088491 |
description | steam transport for the renet crate: Server/Client network library for multiplayer games |
homepage | |
repository | https://github.com/lucaspoffo/renet |
max_upload_size | |
id | 1148709 |
size | 66,599 |
Transport layer for the renet crate using the steamworks-rs.
This crate adds SteamServerTransport
and SteamClientTransport
to replace the default transport layer that comes with renet:
// Setup steam client
let (steam_client, single) = Client::init_app(480).unwrap();
steam_client.networking_utils().init_relay_network_access();
// Create renet server
let connection_config = ConnectionConfig::default();
let mut server: RenetServer = RenetServer::new(connection_config);
// Create steam transport
let access_permission = AccessPermission::Public;
let steam_transport_config = SteamServerConfig {
max_clients: 10,
access_permission,
};
let mut steam_transport = SteamServerTransport::new(&steam_client, steam_transport_config).unwrap();
// Your gameplay loop
loop {
let delta_time = Duration::from_millis(16);
single.run_callbacks(); // Update steam callbacks
server.update(delta_time);
steam_transport.update(&mut server);
// Handle connect/disconnect events
while let Some(event) = server.get_event() {
match event {
ServerEvent::ClientConnected { client_id } => {
println!("Client {} connected.", client_id)
}
ServerEvent::ClientDisconnected { client_id, reason } => {
println!("Client {} disconnected: {}", client_id, reason);
}
}
}
// Code for sending/receiving messages can go here
// Check the examples/demos
steam_transport.send_packets(&mut server);
thread::sleep(delta_time);
}
// Setup steam client
let (steam_client, single) = Client::init_app(480).unwrap();
steam_client.networking_utils().init_relay_network_access();
// Create renet client
let connection_config = ConnectionConfig::default();
let mut client = RenetClient::new(connection_config);
// Create steam transport
let server_steam_id = SteamId::from_raw(0); // Here goes the steam id of the host
let mut steam_transport = SteamClientTransport::new(&steam_client, &server_steam_id).unwrap();
// Your gameplay loop
loop {
let delta_time = Duration::from_millis(16);
single.run_callbacks(); // Update steam callbacks
client.update(delta_time);
steam_transport.update(&mut client);
// Code for sending/receiving messages can go here
// Check the examples/demos
steam_transport.send_packets(&mut client).unwrap();
thread::sleep(delta_time);
}
You can try the steam echo example with (steam needs to be running in the background):
cargo run --example echo server
cargo run --example echo client [HOST_STEAM_ID]
The HOST_STEAM_ID is printed in the console when the server starts.
You can also run the echo example using steam lobbies. Only steam users connected to the lobby will be able to connect to the server. In the echo example, they will first try connect to the steam lobby and later to the renet server:
cargo run --example echo server lobby
cargo run --example echo client [HOST_STEAM_ID] [LOBBY_ID]
The LOBBY_ID is printed in the console when the server starts.
If you are using bevy, you can enable the bevy
feature and instead of using the default transport that comes in bevy_renet
you can use SteamServerPlugin
and SteamClientPlugin
, the setup should be similar.
You can check the Bevy Demo for how to use the default and steam transport switching between them using feature flags.