How do I Parse JSON from a web socket?

0 favourites
  • 4 posts
From the Asset Store
Easily add web monetization to your game and start earning money from supporting visitors.
  • The data I get back from a game server is Json. e.g. If I send the login message, the responses are things like:

    {"message":"loggedIn", "data":{"username":"bob", "balance":100.0, "session":"asdfasd"}}

    {"message":"error","data":{"message":"something went wrong"}}

    {"message":"tick","data":"{very complex data...

    In the websocket->On message I need to be able to do things like

    if ($response.message == "loggedIn") then set txt=$ else if ($response.message == "something_else") then call some function...


    for each ($ -> do something with ($

    Assuming there is a way to parse the websocket.MessageText, how would one do:

    on some event: do A

    if X do Y else do Z

    do B

    I have never figured this out in C2. I am guessing also that each layout can only have one event sheet with one WebSocket On Message in, it as, you cant read from a socket twice and get the same message.

    Surely abitrary json parsing should be native to C2? Or are there so few games accessing game servers or content from other sources?

  • The answer to JSON parsing part is rex_hash: rex rainbow comes to the rescue again. It even handles json arrays, although the resultant code for complex json is not pretty.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • an example using rex_hash

    Here is another example after calling an api and getting an error

    What server software are you using?

  • I do something similar for our chat.

    We are using rex_hash, but I think Yann 's JSON plugin is better supported.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)