Custom Operations

Options
So, I'm new to Photon Server, and I've added a custom operation. I modified the LoadBalancing GameClientPeer.cs from the default server SDK, I made a class with a mysql dll to connect to a database.
My problem right now, is: I need that the client (Using Unity PUN) sends an operation to the server, I tried using the example code from the server docs, and I don't really know where to use that code, like... I need a reference to the networking peer and that's only in PhotonNetwork.cs, right? Also i can't figure out where to put that operation response void.

Any help is apreciated

Thanks in advice

Comments

  • Fufuffu
    Options
    Still couldn't figure it out :( Anything is welcome
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @Fufuffu,

    Thank you for choosing Photon!

    To call your custom operation use NetworkingPeer.OpCustom.

    Also i can't figure out where to put that operation response void.

    You should update NetworkingPeer.OnOperationResponse to handle your custom operation.
  • Fufuffu
    Options
    @JohnTube Thanks you! So aprecciated! Going into it right now :)
  • Fufuffu
    Fufuffu
    edited April 2017
    Options
    Sooo, I had been trying for a time... and I still have problems. I found a way of calling the operation, which was:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon;
    using ExitGames.Client.Photon;
    
    public class DataBaseLogger : PunBehaviour
    {
    
        private PhotonPeer peer;
    
        void Start()
        {
            peer = new PhotonPeer(ConnectionProtocol.Tcp);
    
            var parameter = new Dictionary<byte, object>();
            parameter.Add((byte)100, "test");
            parameter.Add((byte)101, "test");
    
            peer.OpCustom(50, parameter, true);
        }
    }
    
    I created a new peer, as NetworkingPeer.OpCustom() was giving me an error, saying that it needed a reference to a NetworkingPeer.OpCustom(50,parameter,true) non statics.

    Is that the way of doing it???

    Also, how can I update that NetworkingPeer.OnOperationResponse ???

    Sorry for the noob questions :)

    Thanks in advice.

  • Fufuffu
    Fufuffu
    edited April 2017
    Options
    I totally forgot to post this, this code is the modification I made to the GameClientPeer.cs on the LoadBalancing part of the default server sdk

    Line 160:
    switch (request.OperationCode)
                {
                    case (byte)50:
                        var user = (string)request.Parameters[100];
                        var pass = (string)request.Parameters[101];
                        var result = "";
                        
                        if(user != null && pass != null)
                        {
                            result = Photon.LoadBalancing.DatabaseClasses.DBConnect.Login(user, pass);
                        }
    
                        var response = new OperationResponse
                        {
                            OperationCode = request.OperationCode,
                            ReturnCode = 0,
                            DebugMessage = "Sending login values",
                            Parameters = new System.Collections.Generic.Dictionary<byte, object> { { 102, result } }
                        };
                        this.SendOperationResponse(response, sendParameters);
                        return;
    I can also provide the DBConnect class if needed
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    If you are adding a custom operation for user authentication maybe you should consider Custom Authentication feature.

    To implement custom operation in PUN:

    Call Custom Ops from PhotonNetwork:
    public static bool OpCustom(byte customOpCode, Dictionary<byte, object> customOpParameters, bool sendReliable)
        {
            return networkingPeer.OpCustom(customOpCode, customOpParameters, sendReliable);
        }
    You should wrap this method or use something like:
    public static bool CustomOpAuthenticate(string user, string pass)
    {
    byte customOpCode = 50;
    Dictionary<byte, object> = new Dictionary<byte, object> {{(byte)100, user},{(byte)101, pass}};
    return networkingPeer.OpCustom(customOpCode, customOpParameters, true); // better to use overload method that set "encrypt" to true
    }
    Handle response in NetworkingPeer:
    public void OnOperationResponse(OperationResponse operationResponse)
        {
            if (operationResponse.OperationCode == customOperationCode)
            {
                // handle response
                return;
            }
    // ...
    In your server side code, I think you should return an error response (ReturnCode != 0) in case of failure.
  • Fufuffu
    Options
    FInally got it!!! Thank you so much <33 @JohnTube