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}
- Versions: Nakama {3.5}, {Windows, Mac, Linux binary or Docker}, {client library (SDK) and version}
- Server Framework Runtime language (If relevant) {Go, TS/JS, Lua}
{code or log snippet}
Media: