Create json object with integers of type char

VasilisVasilis
edited January 11 in JavaScript
Hi,

I am using this method
function toChar(number) {
  var int8 = new Int8Array(1);
  int8[0] = number;
  return int8[0];
}

to convert an int to a char (1 byte in length). But, when I create the following json object
var obj = {"a_key":tochar(1)};

Then, on the mobile client I see that "a_key" has a value with length of 8 bytes, and not 1.
"{length = 8, bytes = 0x0000000000804840}"

How can I fix this?

Comments

  • I think that the JS SDK does this conversion
  • vadimvadim ✭✭✭
    Hi,
    Json format used for serialization does not support typed arrays. You can use only conventional javascript arrays.
  • Hi,
    Then this is a bug of the JS SDK. I should be able to use a short / char (or byte) integer in a json object.

    On the other hand, I found the Photon.TypeExt class, and it has methods for byte, short, etc.
    Is this what I should use?
  • I used the Photon.TypeExt.Byte, and the message that I got on the mobile client is the same with the one I get when I use none of these functions.

    There has to be a way to send a message that has the correct size.
  • Since it is not possible to send a message with the correct data type from the web client, why is there a doc on how to minimize the size of the message? All this code that I wrote for my mobile apps will go down the drain, and I have to rewrite it!!!
  • vadimvadim ✭✭✭
    Wrap number in object with additional 'type' field and set this field to 1, 2 or 3 depending on integer type you want to send. When deserializing, use this field to get correct type information.
    Which doc you are referencing in your last message?
  • Can you give me an example? Which client will do the deserialization?

    Here is the doc
    https://doc.photonengine.com/en-us/realtime/current/reference/serialization-in-photon

    According to that documentation, the JS client should be able to handle byte and short data types
  • vadimvadim ✭✭✭
    edited January 14
    The document is not relevant to js sdk because js has limited data types set and js client uses json serialization instead of binary protocol.
    So you can send anything that json supports: https://www.json.org/json-en.html
    If type information required, you can send it in wrapping object: Instead of sending 100 as a byte, send {value=100, type=1} object, for 100 as a short, send {value=100, type=2}. Then parse to whatever you want on receiving side.
  • You should put that in the document in bold type so that we are aware of this limitation.

    The purpose of using the byte/short data type is because I want to reduce the size of the message.

    Is it in your plans to use a binary protocol for sending data?
  • vadimvadim ✭✭✭
    Why the size of the message is so important for you? What is the current size and what is your target?
    You can try to pack entire message to one string. Such payload should produce json serialized message of minimal size in theory.
    We have experimental js library implementation which uses the same serialization as native or c# client. But we are not supporting it currently. So in case you switch to it, it might happen that you find a bug and we can't fix it.
  • VasilisVasilis
    edited January 15
    For two reasons:
    Firstly, the smaller the size of the message is the faster it will be transmitted
    Secondly, because I used those data types on the mobile apps. Since the JS client does not support them, I have to release a new version for the mobile apps.

    How buggy is this experimental library? Does this library has the same callbacks with the existing one? If yes, I prefer to use it, than to release a new version for the mobile apps.
  • What is the link to this experimental library?
  • vadimvadim ✭✭✭
    The library works in our demo apps.
    Change import from Photon-Javascript_SDK.js to Photon-Javascript_Emscripten_SDK.js (this file should be in SDK package).
    Now client uses binary protocol and is able to apply types on numbers with methods like Photon.TypeExt.Byte.
  • Great. I will try it.
    Thank you
  • How can I create an Int32[] ?

    I tried this code:
    var actor = [];
    actor[0] = Photon.EmTypesLib.IntAt([inviteeID], 0);
    

    But, I get the following error message:
    (RaiseEventRequest.Actors): should be Int32[] but received Object[].
    
  • vadimvadim ✭✭✭
    To send array of int's, wrap array of numbers with type information like this: TypeExt.Int([1,2,3])
  • I do not believe that it was that simple.
    I tried everything, even the [TypeExt.Int(1)], but I did not think of your solution.

    Thank you!!
  • vadimvadim ✭✭✭
    Glad that it works for you :)
  • VasilisVasilis
    edited January 20
    Thank you!

    After a few logins, I see the following error message:
    Demo: State: JoinedLobby -> ConnectingToGameserver
    Photon-Javascript_Emscripten_SDK.js:5787 WebSocket connection to 'ws://92.38.154.45:9091/6b62cd49-65db-4f4c-9d25-1db9785839e0?protocol=1.6&libid=49&libversion=4.1.9.2' failed: Invalid frame header
    [Em] debugReturn 12020-01-20 22:13:36,519 ERROR   PeerBase.cpp                   onConnect()                                                  line:   420 - 4294967295
    Demo: Game: 2020-01-20 22:13:36,519 ERROR   PeerBase.cpp                   onConnect()                                                  line:   420 - 4294967295
    [Em] onStatusChanged 1023
    Demo: State: ConnectingToGameserver -> Error
    Photon-Javascript_Emscripten_SDK.js:164248 
    Demo: Error: 2002 Game peer connect failed: ws://92.38.154.45:9091
    

    Is this the bug that you mentioned?

    I followed the instructions from https://github.com/gimite/web-socket-js#troubleshooting
    and I am not opening the html page as a local page. But, it did not help
  • vadimvadim ✭✭✭
    I did not mention any specific bug. I'm not aware of any.
    So you joined a room a few times and since then you can't join anymore?
    Can you reproduce this with one of demo apps from sdk package? If yes, can you provide steps required?
  • I did not realize that that was the case, but, yes, I am joining the same room several times.

    I do not think that I will be able to reproduce it with your demo app. This is because when I start the app, all users (from mobile clients) connect to a custom Lobby room.
    I get the above error after the 5th time.
    Could it be because I am not properly disconnecting from the server?
  • vadimvadim ✭✭✭
    edited January 22
    I've never seen such error before, no idea what it is. And I can't reproduce it. Maybe you can capture session with wireshark and send us result?
    Which url you connect initially? ws://ns.exitgames.com:9093 ?
    It makes sense to send us at least full client log with maximum logging level,
  • I will try to, but , I notice the following:
    When I successfully login it makes two requests.
    When it fails, it makes 3 requests

Sign In or Register to comment.