Best way to detect user disconnects

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.

Best way to detect user disconnects

wawro1
2016-07-29 13:26:35

Hi,

we are working on PVP game on iOS. User can turn off his wifi. Problem is that it took almost 10 seconds for client to realize that it was disconnected and getting OnConnectionFailed: TimeoutDisconnect.

We are using those values to set up photon:

  
PhotonNetwork.networkingPeer.DisconnectTimeout = 5000;  
PhotonNetwork.networkingPeer.SentCountAllowance = 9;  
PhotonNetwork.networkingPeer.MaximumTransferUnit = 1500;  

Immediately after turning off wifi I get this error:

  
Cannot send to: 159.8.0.205. An invalid argument was supplied.

UnityEngine.Debug:LogError(Object)  
NetworkingPeer:DebugReturn(DebugLevel, String) (at Assets/Plugins/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1444)  
ExitGames.Client.Photon.<>c__DisplayClass138_0:b__0()  
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()  
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()  
PhotonHandler:Update() (at Assets/Plugins/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:125)

Could it be somehow used to detect disconnect faster ?

I know I can use unity to detect if device is not connected to any network and disable any player interaction, but is there any better way to do this ?

Comments

[Deleted User]
2016-08-01 10:03:28

Hi wawro1,

using the above posted configuration shouldn't take much longer than the given five seconds to realize the disconnect. However you can lower the DisconnectTimeout value to detect disconnects faster, but keep in mind that if the value is too low a network lag might be handled as a disconnect too, which is bad.

Using the error message above can currently not be used to detect disconnects faster, you still have to wait for the OnDisconnectedFromPhoton() callback.

So for now you might using Unity to detect if WiFi gets disabled on the device.

wawro1
2016-08-01 12:29:07

Thanks Christian,

problem for us is current implementation of sending troops to game - it's basic RPC and it's not buffered. So there could be situation where master can send units and client will not receive those rps since master is not connected to internet but thinks that is connected to photon.

So either i need to rework those rpc's to buffered or maybe via server or need to sync instancied objects after somebody reconnects to game.

[Deleted User]
2016-08-01 13:19:55

Maybe I overlooked something but when master client is sending RPCs while not being connected to the internet (even when he thinks that he is connected to Photon) nothing happens. No RPCs are sent or received. In this case you might wait for OnDisconnectedFromPhoton() on the one side and OnPhotonPlayerDisconnected() on the other side (if the game is 1v1), so that the game logic can handle the situation.

wawro1
2016-08-02 12:25:21

Yes if you use PhotonTargets.All in RPC on master client RPC will be executed immediately and copy of it will be sent to others. So I have to use PhotonTargets.AllViaServer so if there is no connection RPC will not executed anywhere and will be lost - and that is problem too.

So here is my question - I'm using UDP, so if RPC is not recieved by server client will not recieve ACK. So every client have list of not executed RPCs. But it looks like that after reconnect those RPC's are cleared. Can I somehow execute those RPC's after player reconnects. We are not using RoomOptions.PlayerTtl and ReJoin but I'm trying to do experiments with it.

Tobias
2016-08-16 09:43:54

If the client didn't successfully send the RPCs, it's the game's task to repeat them.
The send-list gets cleared for the new connection as we can't guess what you want to send again and what should be cleared.

punppis
2016-08-17 10:59:58

I'm also having problems with disconnect detection. I'm simulating DC by disabling by LAN card and I get the following error almost instantly. We're using DisconnectTimeout = 5000.

Cannot send to: 37.58.117.146. An invalid argument was supplied.

Sometimes disconnect never happens. Sometimes it takes 10 seconds, sometimes 30, sometimes over a minute. Disconnect handling is not working correctly and this is a major issue.

PhotonNetwork.connected == true  
PhotonNetwork.connectionState == Connected  
PhotonNetwork.connectionStateDetailed == JoinedLobby

Values above never change when this bug occurs. I wrote this post while all network disabled and Photon still shows me connected.

I just updated from 1.66 to 1.74 hoping it would fix this but the bug still occurs.

JyriKilpelainen
2016-10-10 08:38:32

I'm bumping this one up since I'm experiencing the same thing. Occasionally if I'm connected to Master via Android device's wifi and then disable wifi from the device, the PhotonNetwork.connected stays 'true' and OnDisconnectedFromPhoton is not called. The behavior is really random. Sometimes the event is called but sometimes it's not.

1_Mad_Developer
2016-10-24 07:02:26

Hello!

@Tobias if you read this could you please answer the original question? What is the best way to immediately find out that connection has been lost?

For our game it's not an option to wait 5 -10 seconds before PunBehaviour.OnConnectionFail callback will be called. We need to disable some game UI immdeiately to prevent player from sending events via PhotonNetworking.RaiseEvent

I'm about to write the code that checks network adapter state in corutine and disables UI as soon as internet connection is gone. Please stop me from this madness :)

Thanks!

jeanfabre
2016-10-26 12:47:03

Hi,

Currently, I am not sure there is a built in way to do that easily in PUN. And so yes watching for internet connection outside PUN is a good idea for example:

https://www.assetstore.unity3d.com/en/#!/content/19009

Bye,

Jean

1_Mad_Developer
2016-10-27 01:35:43

Thanks @jeanfabre !

I will follow your advice, but instead of buying an asset I'm going to implement internet availability check on my own using a corutine (greediness is in my blood i guess).

For anyone who has the same problem, here are some links on this topic:

http://answers.unity3d.com/questions/567497/how-to-100-check-internet-availability.html

http://stackoverflow.com/questions/40088731/how-to-check-is-internet-available-without-creating-overhead-in-unity3d

JohnTube
2016-11-10 09:39:15

Hi @1_Mad_Developer,

The solutions suggested in those links do not handle all cases.
Please take a look here.

jeanfabre
2016-11-18 12:11:10

Hi,

also check this for checking internet connectivity:

http://forum.photonengine.com/discussion/8550/photon-not-disconnecting-if-we-disconnect-internet-while-connected#latest

Bye,

Jean

rlonau
2017-06-25 09:41:08

Hi guys,
I know this is an older topic, but I also needed to find a way to handle disconnects. I've got an 1vs1 game and right now this works for both cases (Master disconnects or Client disconnects). The other player gets the info about the disconnect pretty fast.

void Update() { // Here are both PlayerManager scripts if (playerManagerList.Count == 2) { foreach (PlayerManager playerManager in playerManagerList) { // Check for each playerManager if the PhotonPlayer is connected bool isConnected = false; foreach (PhotonPlayer pPlayer in PhotonNetwork.playerList) { if (!pPlayer.IsInactive && pPlayer.UserId == playerManager.playerName) isConnected = true; } // If player is not connected we need to react if (!isConnected) { if (main != null) main.ShowConsoleText("Player not connected: " + playerManager.playerName); } } } }

So I'm checking the PhotonPlayer.IsInactive variable. Just wanted to ask if this is the way to go now or if you guys have other solutions?
Thanks

Back to top