Unity RPC payload send binary without using base64

we are working use proto as RPC payload and want to send binary data as RPC payload.

there is functions for javascript runtime which can convert between binary and string, so is there any method we can convert binary to string in Unity with C# and after server receive the RPC can use the

const buffer = nk.stringToBinary(payload)

to convert string to binary.

we are trying some method base on Go source code of nk.binaryToString, but when we try we always get the error:

{“level”:“error”,“ts”:“2024-05-15T10:49:14.054Z”,“caller”:“server/runtime_javascript.go:573”,“msg”:“JavaScript runtime function raised an uncaught exception”,“mode”:“rpc”,“id”:“protorpc”,“error”:“TypeError: expects data to be UTF-8 encoded at github.com/heroiclabs/nakama/v3/server.(*runtimeJavascriptNakamaModule).mappings.(*runtimeJavascriptNakamaModule).binaryToString.func152 (native)”}

This is the method in C# we convert binary to string: (this is reference of GO implementation:

)

public (string result, Exception error) BinaryToString(byte[] data) {     
  if (data == null)     {         
     return (null, new ArgumentNullException(nameof(data), "expects a byte array"));     
  }      
  try     {         
      // Convert byte array to string using UTF-8 encoding         
      string result = Encoding.UTF8.GetString(data);         
      return (result, null);     
   }     
   catch (Exception ex)    
   {
         return (null, ex);     
   } 
}



We know encoding to base64 will work, but it will increase the traffic 33%,

{Details}

  1. Versions: Nakama {3.5}, {Windows, Mac, Linux binary or Docker}, {client library (SDK) and version}
  2. Server Framework Runtime language (If relevant) {Go, TS/JS, Lua}
{code or log snippet}

:tv: Media:

@mengxin I’m not sure I fully understand the issue, is it that the server is not handling the binary correctly or the client is not able to decode the binary it receives back?

How are you serializing the data to binary in the client before sending it to the server?

Can you give a simple example end-to-end of how you’re sending the data from the client, handling it in the server and then decoding it back in the client?

sure, for example, we define Proto message RpcRequest { some data }

we generate code for Typescript and Unity C#, then in Server and Unity we have ability to encode and decode RpcRequset binary and RpcRequest object.

then we want to send RpcRequest data as payload of Nakama RPC.

currently, we use Base64 encode binary to string.

so on client

RpcRequest Object → proto encode to Binary → Base64 encode to string

set the string as payload to Nakama RPC

server receive the RPC

Base64 decode string to binary → proto decode binary to object.

The Base64 will increase the payload size 33%, So we think whether we can encode binary to string without using Base64?