Game Development Reference
In-Depth Information
Listing 10-1. Some JSON from a Player Profile
{
"PlayerName" : "John McCutchan",
"RecentScores" : [10000, 12345, 99, 1, 0],
"TrophyList": [
{ "TrophyName": "5 in a row", "Unlocked": false },
{ "TrophyName": "First Timer", "Unlocked": true }
]
}
The special characters { and } begin and end a key/value map. The special characters [ and ] begin and end
an ordered list. In Listing 10-1, the root keys are PlayerName , RecentScores , and TrophyList . The value of the
TrophyList key is an ordered list of trophies, each having a TrophyName and an Unlocked flag.
Aside from being the native data structure of browser applications, JSON is easily read and written by humans
with far less markup than, for example, XML.
Now that the data-marshaling format has been fixed, the next piece of the ET-API puzzle is the RPC message
framing, that is, the mandatory portion of the RPC message. Each RPC is a JSON map with two required keys: the
message type and the message serial number or ID, as shown in Listing 10-2.
Listing 10-2. The Message Type and the Message ID
{
"type" : "<type>"
"id" : "<id>"
}
The type field is used to indicate the type of message. ET-API supports three types: command , result , and report .
I will explain each of these shortly.
The ID field is used to pair related messages together. For example, the command shown in Listing 10-3 has an ID of 4.
Listing 10-3. The ID Field Pairs Related Messages Together.
{
"type" : "command",
"id" : "4",
...
}
The result of the command sent back from the engine to the browser application will also have an ID of 4. This ID
makes connecting a function call and return value easy.
Returning to the type field, the possibilities are
Command : Commands initiate some sort of action or state change.
Result : The result of a command. Linked together with the id field. Commands are not
required to reply with a result.
Report : A regular, repeating message from a subscription. Clients can subscribe to report
channels and set an interval between reports.
All other fields in a message are dictated by the requirements of the command. ET-API has three stock commands:
Echo : Results in a reply message containing a copy of the message key.
Subscribe : Caller is subscribed to a report channel.
Unsubscribe : Caller is unsubscribed from a report channel.
 
Search WWH ::




Custom Search