question

brendan avatar image
brendan asked

connect to photon

A.in18dec
started a topic on Fri, 05 June 2015 at 1:52 AM

Hi playfab,

first of all, thanks for making this great services.

I have a problem connecting to photon with playfab.

I have downloaded PUN from the asset store, setup it with the AppId I got from playfab dashboard

here's my code to connect to photon after successfully connected to playfab

 void OnLoginResult(LoginResult result)

 {

  Debug.Log ("Yay, logged in in session token: " + result.SessionTicket);



  PhotonNetwork.AuthValues = new AuthenticationValues();

  PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom;

  PhotonNetwork.AuthValues.SetAuthParameters(result.PlayFabId,result.SessionTicket);

  PhotonNetwork.ConnectToBestCloudServer ("1.0");

 }

and here is the error I got,

Custom Authentication failed (either due to user-input or configuration or AuthParameter string format). Calling: OnCustomAuthenticationFailed()

UnityEngine.Debug:LogError(Object)

NetworkingPeer:OnOperationResponse(OperationResponse) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1145)

ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])

ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()

ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()

PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)

Thanks a lot,

[M. Wahyu Ashari]

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

Best Answer
johntube said on Sat, 06 June 2015 at 7:51 AM

Hi Wahyu Ashari,

I think, you're trying to Connect to Photon using PlayFab's SessionTicket !

What you need to do is to call GetPhotonAuthenticationToken in OnLoginResult. Then you can copy the code you have right now in OnLoginResult into the GetPhotonAuthenticationToken callback and use GetPhotonAuthenticationTokenResult.PhotonCustomAuthenticationToken instead of LoginResult.SessionToken.

Two more things :

  • You might need to do an extra step after PlayFab login and before Photon authentication : GetCloudScriptUrl but I'm not sure (I'm doing it anyway)

  • I'm not sure if PUN supports webhooks RIGHT NOW. In both cases, PUN works only with Photon RealTime so sure you're using a RealTime App Id.

"Talk is cheap show me the code" L.T. :

 void OnLoginResult(LoginResult result)
 {
this.PlayFabId = result.PlayFabId;
GetPhotonToken();
 }

void GetPhotonToken(){
   GetPhotonAuthenticationTokenRequest request = new GetPhotonAuthenticationTokenRequest();
        request.PhotonApplicationId = GameConstants.PhotonAppId.Trim();
        // get an authentication ticket to pass on to Photon
        PlayFabClientAPI.GetPhotonAuthenticationToken(request, OnPhotonAuthenticationSuccess, OnPlayFabError);
}

void OnPhotonTokenReceived(GetPhotonAuthenticationTokenResult result) {

 string photonToken = result.PhotonCustomAuthenticationToken;
Debug.Log (String.Format("Yay, logged in in session token: {0}", photonToken));
  PhotonNetwork.AuthValues = new AuthenticationValues();
  PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom;
  PhotonNetwork.AuthValues.SetAuthParameters(this.PlayFabId, photonToken);
  PhotonNetwork.ConnectToBestCloudServer ("1.0");
    }

6 Comments
johntube said on Sat, 06 June 2015 at 7:51 AM

Hi Wahyu Ashari,

I think, you're trying to Connect to Photon using PlayFab's SessionTicket !

What you need to do is to call GetPhotonAuthenticationToken in OnLoginResult. Then you can copy the code you have right now in OnLoginResult into the GetPhotonAuthenticationToken callback and use GetPhotonAuthenticationTokenResult.PhotonCustomAuthenticationToken instead of LoginResult.SessionToken.

Two more things :

  • You might need to do an extra step after PlayFab login and before Photon authentication : GetCloudScriptUrl but I'm not sure (I'm doing it anyway)

  • I'm not sure if PUN supports webhooks RIGHT NOW. In both cases, PUN works only with Photon RealTime so sure you're using a RealTime App Id.

"Talk is cheap show me the code" L.T. :

 void OnLoginResult(LoginResult result)
 {
this.PlayFabId = result.PlayFabId;
GetPhotonToken();
 }

void GetPhotonToken(){
   GetPhotonAuthenticationTokenRequest request = new GetPhotonAuthenticationTokenRequest();
        request.PhotonApplicationId = GameConstants.PhotonAppId.Trim();
        // get an authentication ticket to pass on to Photon
        PlayFabClientAPI.GetPhotonAuthenticationToken(request, OnPhotonAuthenticationSuccess, OnPlayFabError);
}

void OnPhotonTokenReceived(GetPhotonAuthenticationTokenResult result) {

 string photonToken = result.PhotonCustomAuthenticationToken;
Debug.Log (String.Format("Yay, logged in in session token: {0}", photonToken));
  PhotonNetwork.AuthValues = new AuthenticationValues();
  PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom;
  PhotonNetwork.AuthValues.SetAuthParameters(this.PlayFabId, photonToken);
  PhotonNetwork.ConnectToBestCloudServer ("1.0");
    }

A.in18dec said on Sun, 07 June 2015 at 5:23 AM

wow, so that's how it done..

thanks so much for this..

I finally able to connect to photon,,

about the cloudscript.. thanks for mentioning that too.

still learning about it, didn't try it till now.. it may be useful tips on the future..

thanks :)


Jibran said on Tue, 25 August 2015 at 4:41 AM

where did GameConstants come from?


johntube said on Tue, 25 August 2015 at 11:07 AM

Hi Jibran,

You can ignore GameConstants it's a class from my own code/project.

In order to be up to date with this subject please visit this discussion on PhotonEngine forum.

JT


Jibran said on Tue, 25 August 2015 at 11:05 PM

I was able to do custom authentication in PUN with Playfab through the following code

void OnLoginSuccess(LoginResult result){

        this.PlayFabId=result.PlayFabId;
        Debug.Log("Facebook Login Sucessful");
        GetPhotonToken ();
    }

    void GetPhotonToken(){
        GetPhotonAuthenticationTokenRequest request = new GetPhotonAuthenticationTokenRequest ();
        request.PhotonApplicationId = "";
        PlayFabClientAPI.GetPhotonAuthenticationToken (request, OnPhotonAuthenticated, OnFBPlayfabError);
    }

    void OnPhotonAuthenticated(GetPhotonAuthenticationTokenResult result){

        string photonToken = result.PhotonCustomAuthenticationToken;
        Debug.Log (string.Format ("Yay, logged in in session token: {0}", photonToken));
        PhotonNetwork.AuthValues = new AuthenticationValues ();
        PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom;
        PhotonNetwork.AuthValues.AddAuthParameter ("username", this.PlayFabId);
        PhotonNetwork.AuthValues.AddAuthParameter ("Token", result.PhotonCustomAuthenticationToken);
        PhotonNetwork.AuthValues.UserId = this.PlayFabId;
        PhotonNetwork.ConnectUsingSettings ("1.0");

    }

Now I had a few queries?

See my multiplayer setup is already working. After i login through facebook/playfab, i can create and join rooms and choose random matches.

My problem is that i want to include a matchmaking system where people could play matches with their facebook friends. I was thinking of using playfab push notification system which would send match invite notifications to other player who are playing my game and when the other player clicks the notification the game opens and the player automatically joins the room in which i am waiting and the game starts.

Now my questions are:

  1. Do I need anything more than the custom authentication to integrate playfab with my PUN?

  2. What would be the best way to solve the matchmaking problem I described above?

  3. Do I need to create the rooms through playfab or the PUN room creation systems are ok to acheive the functionalities I want.

It would be a great help if u would clarify all these things for me. If its possible, would you or any of your developers schedule a chat session with me so i can explain my doubts to them and get the clear idea of the things.

Thanx


Brendan Vanous said on Wed, 26 August 2015 at 6:58 PM

Sure thing:

  1. No, you should be good to go.

  2. It sounds like you want to issue challenges to players who may not be playing the game, in which case yes, you would want to use a Push Notification as you describe. Since you're using Photon Cloud, the game sessions are synchronous, so you're waiting on the person to sign in and get the info about the game session they've been invited to, correct? One way to do this would be to create a Shared Group Data object called { {PlayFabId}}_invites for each player, and have players inviting someone to their game to write a Key/Value pair to it of { {Inviting player's PlayFabId}}/{ {Photon Room Name}}. You could then remove it if the player who issued the invite leaves the room.

  3. You use the Photon code to create rooms - you use the AppIDs generated on the PlayFab site and PlayFab authentication.

Hopefully that clarifies everything, but do let us know if you have any other questions.

Brendan

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avimon avatar image avimon commented ·

@A.in18dec How did you check if it's authenticated or not?

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.