How to know if a player is already connected

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.

How to know if a player is already connected

illogiK
2016-04-18 08:32:08

Hi everyone,
Firstly, I'd like to apology for my bad english because i'm a french guy :)

So, here's my problem :

Is there a way to know if a player is already connected to the game ? I'm making a card game, and, users have to register to play. They connect, and then, they play. But, a user can connect multiple time, and I have try with photon friends to know if I am already connected, but, since I get connect, I am connected... I can't know if there are multiple myself...

So any idea ?

Do i have to make it with my database and some Mysql ?
The problem with Mysql is : if user rage quit (alt + f4) i can't send a message to my database to tell "user with id X leave the game".

This is very problematic, because a player can fight with himself !

Thank you

Comments

chvetsov
2016-04-20 08:52:19

hi, there

in our match making we filter out games to which player is already connected. we do this when you call JoinRandomRoom. otherwise you should take care about multiple connections. here guys discuss possible solutions: http://forum.photonengine.com/discussion/7455/server-sdk-4-getting-rid-of-multiple-connections-per-user#latest

illogiK
2016-04-21 13:54:00

Hi chvetsov,
thanks for reply. The problem is that i don't use the server SDK, With the server version, you can trigger multiple connections because users connect to your server. But for me, I connect to PhotonMasterServer, and there're no callbacks if a user is already connected to this one...

chvetsov
2016-04-22 12:44:02

Problem which you tries to solve does not really exist.

As i said i player is in game he will not get it in case of search for random game
if players joins game, where he already is, using JoinOnly/JoinCreateIfNotExist mode, he will be rejected
if player joins game, where he already is, useing RejoinOnly, he will get his previous actor. So, again no problem

you should use authentication and set flag CheckUserOnJoin

illogiK
2016-05-09 07:53:19

I think i didn't explain correctly my problem.
All i want is very simple. If i connect to the game, i want a message "Your already connected" if this is true.
So how to detect this fact ?

JohnTube
2016-05-09 14:10:08

Hi @illogiK,

If by game you mean the actual application and not the room then you may need to use custom authentication to reject already connected players. In that case you will need an external web server that keeps player sessions. You may want to explicitly tell the web server when player logs out (quit, leave) the application using WebRPC. You may also want to handle "extreme situation" where a player gets stuck (not being able to log in to your game) due to session not being closed on web server.

In the future (no ETA) we are thinking about providing such feature out of the box.

You may also consider another way using FindFriends operation but you may need to connect using a special user first to check if the one trying to connect is already connected. Or you can use FindFriends from your web server combined with custom authentication. However, I'm not sure how accurate, reliable and precise is FindFriends currently.

illogiK
2016-05-17 07:50:30

Hi @JohnTube,
thank you for responce !

This is exactly what i wondered. So the FindFriends solution can be very effective. I think i will use this. The problem of WebRPC is : When you quit application, you don't have time to send a web request... So i will be always online:smile:

But, I found a solution with FindFriend. When i connect, i change my PhotonPlayer name temporary, and then, if i find my username, i launch warnings !smile: :smile:

Ps : Find friends work pretty well with Photon !

FessXOR
2021-10-17 22:13:25

Sorry by bad eng.

I found the resolution.

When I click LogIn, I create coroutine, and login with random name, then, by FindFriends, i search NORMAL name of login, if is not exist in online, I relogin with normal LoginName.

Easy

Back to top