How do I Parse JSON from a web socket?

Get help using Construct 2

Post » Mon Mar 06, 2017 11:26 pm

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?
B
17
S
6
G
3
Posts: 370
Reputation: 3,942

Post » Tue Mar 07, 2017 4:55 pm

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.
B
17
S
6
G
3
Posts: 370
Reputation: 3,942

Post » Thu Mar 09, 2017 10:28 pm

an example using rex_hash
Image

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

What server software are you using?
B
25
S
11
G
9
Posts: 136
Reputation: 6,657

Post » Sun Mar 12, 2017 6:48 pm

I do something similar for our chat.

We are using rex_hash, but I think @Yann 's JSON plugin is better supported.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822


Return to How do I....?

Who is online

Users browsing this forum: Darth Crusher and 2 guests