JS realtime communication with PUN

Hi,
I'm building a cross platform app with PUN for Unity and the Javascript Realtime SDK for HTML5. I have successfully gotten the clients to see each other, create rooms, join sessions, etc. I have been unable to get my messaging to work in both directions.

On the original PUN version, I used RPC calls to send Game Events. I was able to receive these via HandleEvent in Javascript.

When attempting to reply from javascript, the only reference I could find in the SDK is for raiseEvent. I was also able to determine that 200 is the event code for RPC calls. Here is where I start to run into trouble...

When the RPC is received, it fails the following NetworkingPeer.cs check:

if (rpcData == null || !rpcData.ContainsKey((byte)0))
{
Debug.LogError("Malformed RPC; this should never occur. Content: " +
SupportClassPun.DictionaryToString(rpcData));
return;
}

The problem is that even when I send something as simple as:

var data = {};
data[0] = 1;
photonClient.raiseEventAll(200, data); //200 == RPC

the 0 arrives as "0" and is != (byte)0

How am I supposed to send Events/RPCs to PUN from the Javascript Realtime SDK?

Thanks!
Rob

Comments

  • hi @RobNBCUNI,

    You have two options:

    1. Switch to custom events/RPCs based on RaiseEvent with a custom code 0..199 instead of PUN's native RPCs (200). This way you can define structure and types.
    2. Change PUN code to handle events incoming from JS clients. I think JS numbers are converted to double, etc.

    I recommend option 1.
  • Hi @JohnTube ,

    Thanks for the response. I had previously tried using a 0..199 number. When I do that, I pass the object check, but wind up in the default / unhandled case of OnEvent. I am unable to override OnEvent on my session as OnEvent isn't part of the IPunCallbacks. So, in either case, I am left to edit the NetworkingPeer.cs internal to the SDK to make this work.

    I consider SDK modification bad practice, and it really just seems as if I'm not understanding the intended usage.

    How can I catch OnEvent in PUN in order to have a custom object?
    How can I send an RPC from Javascript to PUN?
  • Hi @RobNBCUNI,

    How can I catch OnEvent in PUN in order to have a custom object?
    As explained here you need to register OnEvent callback for custom events like this:
    public void OnEnable()
    {
        PhotonNetwork.OnEventCall += OnEvent;
    }
    
    public void OnDisable()
    {
        PhotonNetwork.OnEventCall -= OnEvent;
    }
    
    public void OnEvent(byte eventCode, object content, int senderId)
    {
        // Do something
    }
    How can I send an RPC from Javascript to PUN?
    You are already doing this but you need to modify PUN code to handle events incoming from JavaScript clients as they will contain different types due to serialization limits with JavaScript where types are lost.
  • Hi @JohnTube ,

    That's the solution I arrived on. I didn't initially find it because I was used to using the Photon.Monobehaviour OnXXXX calls. Thanks!

    Rob
Sign In or Register to comment.