How do I Parse JSON from a web socket?

0 favourites
  • 4 posts
From the Asset Store
Web MIDI
$12 USD
Incorporate WebMIDI into your Construct 3 projects
  • 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=$response.data.username else if ($response.message == "something_else") then call some function...

    and

    for each ($respose.data.item) -> do something with ($respose.data.item.name)

    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.

  • an example using rex_hash

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

    What server software are you using?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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)