Setting Up a Networked AR Session
Enable nearby players to join the same AR experience.
Anatomy of an ARNetworking Session
For multiple players to interact in the same AR world, they have to share information from the separate ARSessions running on each of their devices. Connecting players together so they can send and receive that information is handled by a MultipeerNetworking instance on each device. Processing that information is in turn handled by an ARNetworking instance on each device.
ARNetworking
sessions are constructed using the ARNetworkingFactory class.
using Niantic.ARDK.AR.Networking; // Create an ARNetworking session. // This also creates new ARSession and MultipeerNetworking objects, // since they are required components of ARNetworking. var arNetworking = ARNetworkingFactory.Create();
An ARNetworking
session can also be constructed from existing ARSession
or MultipeerNetworking
instances.
For example, your application could have a lobby, where players who are connected to a MulitpeerNetworking
session can send messages to each other while waiting for a shared AR experience to start. The players could be labeled by colors based on their peer Identifier, so when those players hop into the shared AR experience, you want those identifiers to stay the same. Using that same MulitpeerNetworking
session to power the ARNetworking
session enables that, because it means players wouldn’t have to disconnect and join a new session.
using Niantic.ARDK.Networking; using System.Text; var networking = MultipeerNetworkingFactory.Create(); // Can join either before or after creating an ARNetworking var sessionIdentifier = Encoding.UTF8.GetBytes("Example"); networking.Join(sessionIdentifier); // The ARSession and MultipeerNetworking objects used to create an ARNetworking // need to have the same stage identifier, so specify the stage identifier for // the ARSession constructed here. var arSession = ARSessionFactory.Create(networking.StageIdentifier); var arNetworking = ARNetworkingFactory.Create(arSession, networking);
Hosts and Peers
Networked sessions use the concept of host and peer clients. When a client sends a join request with a specified session identifier, the server will do one of the following:
Add the client to the session, if the session exists. The client is now a peer in the session.
Create the session and designate the client as the host.
All subsequent clients that join the same session will know which client is the host.
Each client that wants to join a shared ARDK network session must also be configured to use the same API token.