Struct naia_server::Server
source · pub struct Server<E: Copy + Eq + Hash + Send + Sync> { /* private fields */ }Expand description
A server that uses either UDP or WebRTC communication to send/receive messages to/from connected clients, and syncs registered entities to clients to whom they are in-scope
Implementations§
source§impl<E: Copy + Eq + Hash + Send + Sync> Server<E>
impl<E: Copy + Eq + Hash + Send + Sync> Server<E>
sourcepub fn new<P: Into<Protocol>>(server_config: ServerConfig, protocol: P) -> Self
pub fn new<P: Into<Protocol>>(server_config: ServerConfig, protocol: P) -> Self
Create a new Server
sourcepub fn is_listening(&self) -> bool
pub fn is_listening(&self) -> bool
Returns whether or not the Server has initialized correctly and is listening for Clients
sourcepub fn socket_config(&self) -> &SocketConfig
pub fn socket_config(&self) -> &SocketConfig
Returns socket config
sourcepub fn receive<W: WorldMutType<E>>(&mut self, world: W) -> Events<E>
pub fn receive<W: WorldMutType<E>>(&mut self, world: W) -> Events<E>
Must be called regularly, maintains connection to and receives messages from all Clients
sourcepub fn accept_connection(&mut self, user_key: &UserKey)
pub fn accept_connection(&mut self, user_key: &UserKey)
Accepts an incoming Client User, allowing them to establish a connection with the Server
sourcepub fn reject_connection(&mut self, user_key: &UserKey)
pub fn reject_connection(&mut self, user_key: &UserKey)
Rejects an incoming Client User, terminating their attempt to establish a connection with the Server
sourcepub fn send_message<C: Channel, M: Message>(
&mut self,
user_key: &UserKey,
message: &M,
)
pub fn send_message<C: Channel, M: Message>( &mut self, user_key: &UserKey, message: &M, )
Queues up an Message to be sent to the Client associated with a given UserKey
sourcepub fn broadcast_message<C: Channel, M: Message>(&mut self, message: &M)
pub fn broadcast_message<C: Channel, M: Message>(&mut self, message: &M)
Sends a message to all connected users using a given channel
pub fn send_request<C: Channel, Q: Request>( &mut self, user_key: &UserKey, request: &Q, ) -> Result<ResponseReceiveKey<Q::Response>, NaiaServerError>
sourcepub fn send_response<S: Response>(
&mut self,
response_key: &ResponseSendKey<S>,
response: &S,
) -> bool
pub fn send_response<S: Response>( &mut self, response_key: &ResponseSendKey<S>, response: &S, ) -> bool
Sends a Response for a given Request. Returns whether or not was successful.
pub fn receive_response<S: Response>( &mut self, response_key: &ResponseReceiveKey<S>, ) -> Option<(UserKey, S)>
pub fn receive_tick_buffer_messages( &mut self, tick: &Tick, ) -> TickBufferMessages
sourcepub fn scope_checks(&self) -> Vec<(RoomKey, UserKey, E)>
pub fn scope_checks(&self) -> Vec<(RoomKey, UserKey, E)>
Used to evaluate whether, given a User & Entity that are in the same Room, said Entity should be in scope for the given User.
While Rooms allow for a very simple scope to which an Entity can belong, this provides complete customization for advanced scopes.
Return a collection of Entity Scope Sets, being a unique combination of a related Room, User, and Entity, used to determine which Entities to replicate to which Users
sourcepub fn send_all_updates<W: WorldRefType<E>>(&mut self, world: W)
pub fn send_all_updates<W: WorldRefType<E>>(&mut self, world: W)
Sends all update messages to all Clients. If you don’t call this method, the Server will never communicate with it’s connected Clients
sourcepub fn spawn_entity<W: WorldMutType<E>>(
&mut self,
world: W,
) -> EntityMut<'_, E, W>
pub fn spawn_entity<W: WorldMutType<E>>( &mut self, world: W, ) -> EntityMut<'_, E, W>
Creates a new Entity and returns an EntityMut which can be used for further operations on the Entity
sourcepub fn enable_entity_replication(&mut self, entity: &E)
pub fn enable_entity_replication(&mut self, entity: &E)
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
sourcepub fn disable_entity_replication(&mut self, entity: &E)
pub fn disable_entity_replication(&mut self, entity: &E)
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
pub fn pause_entity_replication(&mut self, entity: &E)
pub fn resume_entity_replication(&mut self, entity: &E)
sourcepub fn entity_replication_config(&self, entity: &E) -> Option<ReplicationConfig>
pub fn entity_replication_config(&self, entity: &E) -> Option<ReplicationConfig>
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
pub fn configure_entity_replication<W: WorldMutType<E>>( &mut self, world: &mut W, entity: &E, config: ReplicationConfig, )
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
This is used only for Hecs/Bevy adapter crates, do not use otherwise!
sourcepub fn entity<W: WorldRefType<E>>(
&self,
world: W,
entity: &E,
) -> EntityRef<'_, E, W>
pub fn entity<W: WorldRefType<E>>( &self, world: W, entity: &E, ) -> EntityRef<'_, E, W>
Retrieves an EntityRef that exposes read-only operations for the Entity. Panics if the Entity does not exist.
sourcepub fn entity_mut<W: WorldMutType<E>>(
&mut self,
world: W,
entity: &E,
) -> EntityMut<'_, E, W>
pub fn entity_mut<W: WorldMutType<E>>( &mut self, world: W, entity: &E, ) -> EntityMut<'_, E, W>
Retrieves an EntityMut that exposes read and write operations for the Entity. Panics if the Entity does not exist.
sourcepub fn entities<W: WorldRefType<E>>(&self, world: W) -> Vec<E>
pub fn entities<W: WorldRefType<E>>(&self, world: W) -> Vec<E>
Gets a Vec of all Entities in the given World
pub fn entity_owner(&self, entity: &E) -> EntityOwner
sourcepub fn user_exists(&self, user_key: &UserKey) -> bool
pub fn user_exists(&self, user_key: &UserKey) -> bool
Returns whether or not a User exists for the given RoomKey
sourcepub fn user(&self, user_key: &UserKey) -> UserRef<'_, E>
pub fn user(&self, user_key: &UserKey) -> UserRef<'_, E>
Retrieves an UserRef that exposes read-only operations for the User associated with the given UserKey. Panics if the user does not exist.
sourcepub fn user_mut(&mut self, user_key: &UserKey) -> UserMut<'_, E>
pub fn user_mut(&mut self, user_key: &UserKey) -> UserMut<'_, E>
Retrieves an UserMut that exposes read and write operations for the User associated with the given UserKey. Returns None if the user does not exist.
sourcepub fn users_count(&self) -> usize
pub fn users_count(&self) -> usize
Get the number of Users currently connected
sourcepub fn user_scope(&self, user_key: &UserKey) -> UserScopeRef<'_, E>
pub fn user_scope(&self, user_key: &UserKey) -> UserScopeRef<'_, E>
Returns a UserScopeRef, which is used to query whether a given user has
sourcepub fn user_scope_mut(&mut self, user_key: &UserKey) -> UserScopeMut<'_, E>
pub fn user_scope_mut(&mut self, user_key: &UserKey) -> UserScopeMut<'_, E>
Returns a UserScopeMut, which is used to include/exclude Entities for a given User
sourcepub fn make_room(&mut self) -> RoomMut<'_, E>
pub fn make_room(&mut self) -> RoomMut<'_, E>
Creates a new Room on the Server and returns a corresponding RoomMut, which can be used to add users/entities to the room or retrieve its key
sourcepub fn room_exists(&self, room_key: &RoomKey) -> bool
pub fn room_exists(&self, room_key: &RoomKey) -> bool
Returns whether or not a Room exists for the given RoomKey
sourcepub fn room(&self, room_key: &RoomKey) -> RoomRef<'_, E>
pub fn room(&self, room_key: &RoomKey) -> RoomRef<'_, E>
Retrieves an RoomMut that exposes read and write operations for the Room associated with the given RoomKey. Panics if the room does not exist.
sourcepub fn room_mut(&mut self, room_key: &RoomKey) -> RoomMut<'_, E>
pub fn room_mut(&mut self, room_key: &RoomKey) -> RoomMut<'_, E>
Retrieves an RoomMut that exposes read and write operations for the Room associated with the given RoomKey. Panics if the room does not exist.
sourcepub fn rooms_count(&self) -> usize
pub fn rooms_count(&self) -> usize
Get a count of how many Rooms currently exist
sourcepub fn current_tick(&self) -> Tick
pub fn current_tick(&self) -> Tick
Gets the current tick of the Server
sourcepub fn average_tick_duration(&self) -> Duration
pub fn average_tick_duration(&self) -> Duration
Gets the current average tick duration of the Server
pub fn outgoing_bandwidth_total(&mut self) -> f32
pub fn incoming_bandwidth_total(&mut self) -> f32
pub fn outgoing_bandwidth_to_client(&mut self, address: &SocketAddr) -> f32
pub fn incoming_bandwidth_from_client(&mut self, address: &SocketAddr) -> f32
sourcepub fn rtt(&self, user_key: &UserKey) -> Option<f32>
pub fn rtt(&self, user_key: &UserKey) -> Option<f32>
Gets the average Round Trip Time measured to the given User’s Client
sourcepub fn jitter(&self, user_key: &UserKey) -> Option<f32>
pub fn jitter(&self, user_key: &UserKey) -> Option<f32>
Gets the average Jitter measured in connection to the given User’s Client