Exemplo n.º 1
0
        public static async UniTask ConnectToMasterAsync(
            string masterServerAddress,
            int port,
            string appID,
            CancellationToken token = default)
        {
            if (PhotonNetwork.IsConnected)
            {
                return;
            }

            var task = UniTask.WhenAny(
                Pun2TaskCallback.OnConnectedToMasterAsync().AsAsyncUnitUniTask(),
                Pun2TaskCallback.OnDisconnectedAsync());

            PhotonNetwork.ConnectToMaster(masterServerAddress, port, appID);

            var(winIndex, _, disconnectCause) = await task.WithCancellation(token);

            if (winIndex == 0)
            {
                return;
            }
            throw new ConnectionFailedException(disconnectCause);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Makes this client disconnect from the photon server, a process that leaves any room and calls OnDisconnected on completion.
 /// </summary>
 /// <remarks>
 /// When you disconnect, the client will send a "disconnecting" message to the server. This speeds up leave/disconnect
 /// messages for players in the same room as you (otherwise the server would timeout this client's connection).
 /// When used in OfflineMode, the state-change and event-call OnDisconnected are immediate.
 /// Offline mode is set to false as well.
 /// Once disconnected, the client can connect again. Use ConnectUsingSettings.
 /// </remarks>
 public static async UniTask DisconnectAsync()
 {
     if (PhotonNetwork.NetworkClientState == ClientState.Disconnected)
     {
         return;
     }
     PhotonNetwork.Disconnect();
     await Pun2TaskCallback.OnDisconnectedAsync();
 }
Exemplo n.º 3
0
        public static async UniTask ConnectUsingSettingsAsync(
            AppSettings appSettings,
            bool startInOfflineMode = false,
            CancellationToken token = default)
        {
            var task = UniTask.WhenAny(
                Pun2TaskCallback.OnConnectedToMasterAsync().AsAsyncUnitUniTask(),
                Pun2TaskCallback.OnDisconnectedAsync());

            PhotonNetwork.ConnectUsingSettings(appSettings, startInOfflineMode);

            var(winIndex, _, disconnectCause) = await task.WithCancellation(token);

            if (winIndex == 0)
            {
                return;
            }
            throw new ConnectionFailedException(disconnectCause);
        }
Exemplo n.º 4
0
        /// <summary>Can be used to reconnect to the master server after a disconnect.</summary>
        /// <remarks>
        /// After losing connection, you can use this to connect a client to the region Master Server again.
        /// Cache the room name you're in and use RejoinRoom(roomname) to return to a game.
        /// Common use case: Press the Lock Button on a iOS device and you get disconnected immediately.
        /// </remarks>
        public static async UniTask ReconnectAsync(CancellationToken token = default)
        {
            if (PhotonNetwork.IsConnected)
            {
                return;
            }

            var task = UniTask.WhenAny(
                Pun2TaskCallback.OnConnectedToMasterAsync().AsAsyncUnitUniTask(),
                Pun2TaskCallback.OnDisconnectedAsync());

            PhotonNetwork.Reconnect();

            var(winIndex, _, disconnectCause) = await task.WithCancellation(token);

            if (winIndex == 0)
            {
                return;
            }
            throw new ConnectionFailedException(disconnectCause);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Connects to the Photon Cloud region of choice.
        /// </summary>
        /// <remarks>
        /// It's typically enough to define the region code ("eu", "us", etc).
        /// Connecting to a specific cluster may be necessary, when regions get sharded and you support friends / invites.
        ///
        /// In all other cases, you should not define a cluster as this allows the Name Server to distribute
        /// clients as needed. A random, load balanced cluster will be selected.
        ///
        /// The Name Server has the final say to assign a cluster as available.
        /// If the requested cluster is not available another will be assigned.
        ///
        /// Once connected, check the value of CurrentCluster.
        /// </remarks>
        public static async UniTask ConnectToRegionAsync(string region, CancellationToken token = default)
        {
            if (PhotonNetwork.IsConnected)
            {
                return;
            }

            var task = UniTask.WhenAny(
                Pun2TaskCallback.OnConnectedToMasterAsync().AsAsyncUnitUniTask(),
                Pun2TaskCallback.OnDisconnectedAsync());

            PhotonNetwork.ConnectToRegion(region);

            var(winIndex, _, disconnectCause) = await task.AttachExternalCancellation(token);

            if (winIndex == 0)
            {
                return;
            }
            throw new ConnectionFailedException(disconnectCause);
        }