|
Kanda Foundation 0.8.0
|
There are multiple ways to play sessions in the Kanda SDK. Sessions can be joined offline, connected to a dedicated server, or hosted by clients themselves, allowing other clients to join via relay or LAN.
In this context, client refers to code that joins and participates in a session, while server refers to code that hosts a session for one or more clients.
Each play mode corresponds to a specific strategy in the AppLifecycleService:
IOfflineSessionStrategyILanSessionStrategyIRelaySessionStrategyIServerSessionHostingStrategyIServerSessionJoiningStrategyWhen deciding which hosting mode to use, consider the following factors:
Each mode has its strengths and is suited for different scenarios. Choose the one that best fits your application's needs and infrastructure capabilities.
Local sessions (offline, LAN, or lobby) run client and server code in separate processes that communicate via localhost, making them effectively "multiplayer" sessions. This means local sessions support the full feature set of networked multiplayer, since the architecture is identical.
When hosting sessions locally, we need to select a port for network communication. This is handled through a dynamic port selection strategy.
Configuration is managed in Kanda SDK project settings by specifying minimum and maximum port values. Default range is typically 7000-8000 and can be adjusted based on your environment's requirements.
We search the configured range sequentially and test each port for availability using UDP, then use the first available port found. If no ports are available in the selected range, the session will fail to start.
Consider adjusting the port range if conflicts occur frequently in your deployment environment.
To establish secure connections or to enable use on restrictive firewalls for internet-based sessions, you can enable Connect Using Websockets in the Kanda SDK project settings.
When this flag is enabled, client apps establish the connection using the Secure WebSocket (WSS) protocol. In this context, connections appear as secure TLS traffic at the cost of degraded performance.
Client-hosted sessions use Unity Relay by default. When Connect Using Websockets is enabled, the relay connection is configured to use WSS for all connected clients, including the host.
How we implement this can be observed in UnityRelayClientSessionStrategy.
We host servers with Unity Multiplay Hosting by default. Multiplay machines are dynamically allocated and, at the time of writing, don't provide certificates per server.
We work around these limitations to provide WSS for servers by communicating with it via Unity Relay. The relay service supports multiple connection protocols. On startup, dedicated servers with Connect Using Websockets will allocate a Relay server, connect to it over UDP with DTLS encryption, and share the Relay join code to clients by updating its Room Server entry in Kanda Cloud.
When connecting to a Room Server, clients can obtain the relay join code and connect to it via WSS. This way, we achieve end-to-end encryption when communicating with dedicated servers.
How we implement this can be observed in KandaCloudServerSessionHostingStrategy and UnityRelayServerSessionJoiningStrategy.
While the specific implementation details may vary, session strategies typically handle these steps:
Session strategies implement these steps according to their specific hosting mode requirements.
You can customize the behavior of each play mode by overriding its corresponding strategy in the AppLifecycleService. This allows you to adapt the session management to your specific needs without modifying the core SDK code.
Example of customizing the offline hosting mode:
By overriding these strategies, you can implement custom logic for network discovery, session initialization, or any other aspect of session management specific to your app requirements.
This can be useful in cases where you want to implement alternative means of hosting sessions, such as using an alternative hosting provider or integrating to custom services.