PUN Callbacks are sometimes not calling back.

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

PUN Callbacks are sometimes not calling back.

SteelWindows
2021-07-02 01:02:30

I'm using the cloud server for a multiple player project I'm working on and I notice that my Unity client will sometimes not receive PUN callbacks.

I'll post the code I'm using below. It's all consolidated to a single class.

So my Photon connection delegation works like this: At Start I connect automatically. I get On Connected To Master callback then I attempt to Join Random Room. On Random Room Join Failed, I then attempts to create a room assuming that one doesn't exist. Create Room then gets called, makes the room, but then I get no more callbacks. No OnCreatedRoom. No On Joined Room. Nothing else happens.

This is an intermittent thing where callbacks just stop and I'm not sure how to detect a could service error I get no feedback on.

This multiplayer setup is very simple. I'm not taking advantage of matchmaking, or hosting multiple rooms or lobbies. I'm just trying to get a shared scene online that my clients connect to automatically where Unity Game Object movement is synchronized across multiple AR devices.

public class PhotonRoom : MonoBehaviourPunCallbacks, IInRoomCallbacks {

   public static PhotonRoom Instance;

   public GameObject playerNodePrefab = default;

   Player[] photonPlayers;  
   int roomNumber = 1;  
   int userIdCount;  
   int playersInRoom;  
   int myNumberInRoom;

   void Start () {

      // Allow prefabs not in a Resources folder  
      if (PhotonNetwork.PrefabPool is DefaultPool pool) {  
         if (playerNodePrefab != null) pool.ResourceCache.Add(playerNodePrefab.name, playerNodePrefab);  
      }

      PhotonNetwork.ConnectUsingSettings();  
   }

   void Awake () {  
      Instance = this;  
   }

   void StartGame () {  
      CreatePlayer();  
   }

   void CreateRoom () {  
      var roomOptions = new RoomOptions {IsVisible = true, IsOpen = true, MaxPlayers = 20};  
      PhotonNetwork.CreateRoom("Room" + UnityEngine.Random.Range(1, 3000), roomOptions);  
   }

   void CreatePlayer () {  
      var playerNodeObject = PhotonNetwork.Instantiate(playerNodePrefab.name, Vector3.zero, Quaternion.identity);  
   }

   public override void OnConnectedToMaster () {  
      var randomUserId = UnityEngine.Random.Range(0, 999999);  
      PhotonNetwork.AutomaticallySyncScene = false;  
      PhotonNetwork.AuthValues = new AuthenticationValues();  
      PhotonNetwork.AuthValues.UserId = randomUserId.ToString();  
      userIdCount++;  
      PhotonNetwork.NickName = PhotonNetwork.AuthValues.UserId;  
      PhotonNetwork.JoinRandomRoom();  
   }

   public override void OnJoinRandomFailed (short returnCode, string message) {  
      Debug.Log("PhotonRoom.OnJoinRandomFailed()", LOG_LEVEL);  
      CreateRoom();  
   }

   public override void OnCreateRoomFailed (short returnCode, string message) {  
      Debug.Log("PhotonRoom.OnCreateRoomFailed()", LOG_LEVEL);  
      CreateRoom();  
   }

   public override void OnCreatedRoom () {  
      base.OnCreatedRoom();  
      roomNumber++;  
   }

   public void OnCancelButtonClicked () {  
      PhotonNetwork.LeaveRoom();  
   }

   public override void OnPlayerEnteredRoom (Player newPlayer) {  
      base.OnPlayerEnteredRoom(newPlayer);

      photonPlayers = PhotonNetwork.PlayerList;  
      playersInRoom++;  
   }

   public override void OnEnable () {  
      base.OnEnable();

      PhotonNetwork.AddCallbackTarget(this);  
   }

   public override void OnDisable () {  
      base.OnDisable();

      PhotonNetwork.RemoveCallbackTarget(this);  
   }

   public override void OnJoinedRoom () {  
      base.OnJoinedRoom();

      Debug.Log("PhotonRoom.OnJoinedRoom()", LOG_LEVEL);  
      Debug.Log("Current room name: " + PhotonNetwork.CurrentRoom.Name, LOG_LEVEL);  
      Debug.Log("Other players in room: " + PhotonNetwork.CountOfPlayersInRooms, LOG_LEVEL);  
      Debug.Log("Total players in room: " + (PhotonNetwork.CountOfPlayersInRooms + 1), LOG_LEVEL);

      photonPlayers = PhotonNetwork.PlayerList;  
      playersInRoom = photonPlayers.Length;  
      myNumberInRoom = playersInRoom;  
      PhotonNetwork.NickName = myNumberInRoom.ToString();

      StartGame();  
   }  
}  

Comments

Alarack
2021-07-02 02:11:50

I'm having a very similar issue where sometimes when I create a room, there's no callback at all, and this is because the room is never joined to begin with.

No OnJoinedRoomFailed gets called either. It just times out and disconnects.

I've been going crazy over this for days now.

Tobias
2021-07-02 10:30:30

@Alarack
If there is a timeout, this may in fact cause the other callbacks to never fire. It's not a "could not join a room" problem when the connection is gone...
This page about Analyzing Disconnects may help figure out what's up.

@SteelWindows, is there a disconnect callback in your case?
If this reproduces, please enable the SupportLogger and (in best case) run Wireshark along with the repro attempts. Send the full log and capture to: [email protected] please. We'd like to take a look.

In general: The most common cause to not get callbacks is that objects that should register for callbacks, don't register in the first place or get destroyed.
Which region do you connect to?

Edit: Update PUN 2 to the latest version, if you didn't yet. Let us know the platform you build for.

SteelWindows
2021-07-02 15:01:38

@Tobias
So far I have not been able to reproduce it today. I did some thorough regression tests connecting and disconnecting through API calls looking for an OnRoomJoined callback that doesn't make it. But so far I have 100% success out of 100 trials.

I'm guessing last night's oddness was a fluke with the cloud server. Maybe a packet over the greater internet was lost or something strange. I'm expecting to see it again in the next few days because that seem to be it's rate of reoccurrence. It seems to happen when it happens and getting it to reproduce through stress testing doesn't make it happen.

JohnTube
2021-07-06 10:40:18

Hi @SteelWindows,

Thank you for choosing Photon!

There are some small mistake in the code:

  • First, when you extend MonoBehaviourPunCallbacks no need to implement IInRoomCallbacks interface as MonoBehaviourPunCallbacks already does that.
  • Second, when you extend MonoBehaviourPunCallbacks and override OnEnable and OnDisable and call respective base class methods no need to call AddCallbackTarget or RemoveCallbackTarget respectively. This may cause the callbacks to be registered and called twice?!

So: I would get rid of IInRoomCallbacks, OnEnable and OnDisable.

I did not read all the thread and all details but this could be related maybe?

Back to top