Search Unity

Third Party How to send messages from client (using PUN) to Photon Server

Discussion in 'Multiplayer' started by zee_ola05, Feb 9, 2017.

  1. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    My game is a card game. I want my clients to send their input/actions to the server. And have the server validate these actions, then send acknowledgments back to the client. But I can't seem to find a way to send messages from client to server.

    Is there a way for PUN to send messages to Photon Server? So far I got these:
    Code (CSharp):
    1. PhotonView photonView = PhotonView.Get(this);
    2. photonView.RPC("SomeFunction", PhotonTargets.All, "parameter1");
    Code (CSharp):
    1. PhotonNetwork.RaiseEvent(evCode, content, reliable, null);
    But both don't have a way of specifying sending it to Server Only.

    How do you guys do it?
     
    hopetolive likes this.
  2. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    After a few digging, I've found that you can send operations to the server through PhotonPeer.OpCustom() method. In PUN, PhotonPeer (NetworkingPeer) is a private member variable of PhotonNetwork. PhotonNetwork is basically just a wrapper of PhotonPeer. However, PhotonNetwork doesn't expose PhotonPeer nor does it provide an interface to call OpCustom().

    So the problem still persists, how do I send Custom Operations using PUN?
     
  3. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Okay, I was wrong when I said that PhotonNetwork's PhotonPeer's reference is private. It is actually internal.

    So you could send an operation to the server using:
    Code (CSharp):
    1. PhotonNetwork.networkingPeer.OpCustom(opCode, parameters, isReliable);
    I guess this issue is solved then! :)
     
  4. ChristianSimon

    ChristianSimon

    Joined:
    Jun 20, 2016
    Posts:
    8
    Hi @zee_ola05,

    good to hear you found a way to do what you want to achieve. Maybe I can give you some additional input. You told us that you use Photon Server. This can be extended with Plugins which offers you 9 predefined hooks where you can inject server-side custom logic. One of these hooks is:
    Code (CSharp):
    1. OnRaiseEvent(IRaiseEventCallInfo info)
    As you already noticed by the name, this allows you to respond to client-side RaiseEvent(...) calls and allows the server to check the content of the message by the transmitted event code and to forward the message to other clients or to cancel and return an error message to the sending client.

    Plugins are described here.
     
  5. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Just an update, I ended up not using PUN. I am using their Unity SDK instead, which PUN is based off of. The reason is because I felt like PUN is designed for Photon Cloud (without server code). I needed more control so instead of working around PUN, I just used the simpler Photon Unity SDK instead.
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Thanks for taking the time to actually work with PUN and our more bare-bones Unity Client SDK.
    Your impression is about right: While you could work your way "up" with the Unity SDK, PUN adds several comfy layers on top of the low-level Photon API and you need to dig somewhat to get back most of the control you got otherwise. It depends on preference, which package is best for you.

    You were on the right track to use the NetworkingPeer directly. It's a needed "evil" to hide it somewhat in PUN, so it's not accidentally used directly, unless you know what you do.
    Hope you're making good progress.