question

maulik lathyia avatar image
maulik lathyia asked

Photon webhooks not wokring

I did everything as shown in the guide but no events on play stream I tried everything but can't figure it out.

https://docs.microsoft.com/en-gb/gaming/playfab/features/multiplayer/photon/quickstart


Title id: B650D

webhook settings


Unity Logs :
https://prnt.sc/r25l3x

Code C#

using System.Collections.Generic;
using Photon.Pun;
using Photon.Realtime;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;


public class PlayFabAuthenticator : MonoBehaviourPunCallbacks
{


    public string _playFabPlayerIdCache;




    //Run the entire thing on awake
    public void Awake()
    {
          AuthenticateWithPlayFab();
        //  DontDestroyOnLoad(gameObject);
       
    }




    /*
     * Step 1
     * We authenticate current PlayFab user normally.
     * In this case we use LoginWithCustomID API call for simplicity.
     * You can absolutely use any Login method you want.
     * We use PlayFabSettings.DeviceUniqueIdentifier as our custom ID.
     * We pass RequestPhotonToken as a callback to be our next step, if
     * authentication was successful.
     */
    private void AuthenticateWithPlayFab()
    {
        Debug.Log("PlayFab authenticating using Custom ID...");


        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
        {
            CreateAccount = true,
            CustomId = PlayFabSettings.DeviceUniqueIdentifier + "EDITOR"
        }, RequestPhotonToken, OnPlayFabError);
    }


    /*
    * Step 2
    * We request Photon authentication token from PlayFab.
    * This is a crucial step, because Photon uses different authentication tokens
    * than PlayFab. Thus, you cannot directly use PlayFab SessionTicket and
    * you need to explicitly request a token. This API call requires you to
    * pass Photon App ID. App ID may be hard coded, but, in this example,
    * We are accessing it using convenient static field on PhotonNetwork class
    * We pass in AuthenticateWithPhoton as a callback to be our next step, if
    * we have acquired token successfully
    */
    private void RequestPhotonToken(LoginResult obj)
    {
        Debug.Log("PlayFab authenticated. Requesting photon token...");


        //We can player PlayFabId. This will come in handy during next step
        _playFabPlayerIdCache = obj.PlayFabId;


        PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
        {
            PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime
        }, AuthenticateWithPhoton, OnPlayFabError);
    }


    /*
     * Step 3
     * This is the final and the simplest step. We create new AuthenticationValues instance.
     * This class describes how to authenticate a players inside Photon environment.
     */
    private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult obj)
    {
        Debug.Log("Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");


        //We set AuthType to custom, meaning we bring our own, PlayFab authentication procedure.
        var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };


        //We add "username" parameter. Do not let it confuse you: PlayFab is expecting this parameter to contain player PlayFab ID (!) and not username.
        customAuth.AddAuthParameter("username", _playFabPlayerIdCache);    // expected by PlayFab custom auth service


        //We add "token" parameter. PlayFab expects it to contain Photon Authentication Token issues to your during previous step.
        customAuth.AddAuthParameter("token", obj.PhotonCustomAuthenticationToken);


        //We finally tell Photon to use this authentication parameters throughout the entire application.
        PhotonNetwork.AuthValues = customAuth;
        PhotonNetwork.ConnectUsingSettings();


    }


    private void OnPlayFabError(PlayFabError obj)
    {
        Debug.Log(obj.ErrorMessage);
    }


   


    // Add small button to launch our example code
    public void OnGUI()
    {
        if (GUILayout.Button("Execute Example ")) ExecuteExample();
    }


    public override void OnConnectedToMaster()
    {
        Debug.Log("connected  to master");
        PhotonNetwork.JoinOrCreateRoom("TestRoom", new RoomOptions() { }, TypedLobby.Default);


    }


   


    public override void OnCreatedRoom()
    {
        Debug.Log("room created");
    }


    public override void OnJoinedRoom()
    {
        Debug.Log("Joined room");
    }




    // Example code which raises custom room event, then sets custom room property
    private void ExecuteExample()
    {


        // Raise custom room event
        //var data = new Dictionary<string, object>() { { "Hello", "World" } };
        //var result = PhotonNetwork.RaiseEvent(15, data, true, new RaiseEventOptions()
        //{
        //    ForwardToWebhook = true,
        //});
        //LogMessage("New Room Event Post: " + result);


        // Set custom room property
        var properties = new ExitGames.Client.Photon.Hashtable() { { "CustomProperty", "It's Value" } };
        var expectedProperties = new ExitGames.Client.Photon.Hashtable();
        PhotonNetwork.CurrentRoom.SetCustomProperties(properties, expectedProperties);
        Debug.Log("New Room Properties Set");
    }


}
photonwebhooks
download.png (96.3 KiB)
stream.png (72.6 KiB)
10 |1200

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

maulik lathyia avatar image
maulik lathyia answered

Solution


Webhooks like "room join", "room left", "room created" works like a charm. But for Room event & Room Options Update, you need to do have

1 "IsPersistent" true in photon app settings

2 while updating room properties or raising event you need to mention WebFlags

I do it like this

Unity c#

Event

 public void SendEvent(byte _id,object[] _data)
    {
        var flags = new WebFlags(WebFlags.HttpForwardConst);
        RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Flags = flags,  Receivers = ReceiverGroup.All };
        SendOptions sendOptions = new SendOptions { Reliability = true };       
        PhotonNetwork.RaiseEvent(_id, _data, raiseEventOptions, sendOptions);
    }

Room Options

  public void SetRoomProperties(string _key,string _value)
    {
        if (PhotonNetwork.InRoom)
        {           
            ExitGames.Client.Photon.Hashtable roomProps = new ExitGames.Client.Photon.Hashtable();
            roomProps.Add(_key,_value);
            var expectedProperties = new ExitGames.Client.Photon.Hashtable();
            var flags = new WebFlags(WebFlags.HttpForwardConst);
            PhotonNetwork.CurrentRoom.SetCustomProperties(roomProps, expectedProperties, flags);
        }
        else
        {
            Debug.LogError("Not in room");
        }            
    }
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.

Hamza Lazaar avatar image Hamza Lazaar commented ·

Hi @maulik lathyia

Thank you for choosing Photon!

I'm glad you managed to find a solution on your own.
However, I wanted to clarify a small misunderstanding:

"IsPersistent" is not needed to forward RaiseEvent or SetProperties requests as HTTP webhooks.

1 Like 1 ·
Citrus Yan avatar image Citrus Yan commented ·

Thanks for this info:)

0 Likes 0 ·
maulik lathyia avatar image
maulik lathyia answered

It's Solved

1 comment
10 |1200

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

Jordan avatar image Jordan ♦ commented ·

@maulik lathyia Glad to hear you solved it! Would you mind giving a brief description of the solution you found for future reference?

1 Like 1 ·

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.