6.x Java Extensions CookBook

This document provides quick snippets of code organized by theme that will get you started with most of the server side coding tasks. Feel free to suggest more "recipes" by sending us an email or posting in our support forums

» Using JSON Objects and protocol

While ActionscriptObjects are serialized in XML format, you can also use the more compact JSON serialization and protocol available from both client and server side. The following recipes are based on the ActionscriptObject examples.

Recipe #1:

The client sends an object containing a string and a number. The sfs object is a instance of the SmartFoxClient class and the three parmeters passed to the sendXtMessage() method are: the name of the extension, the "command" name and the parameters object.

var obj:Object = {}
obj.str = "Hello World!"
obj.num = 54321

sfs.sendXtMessage("test", "cmd", obj, "json")

Note that the in the sendXtMessage() 4th argument we have specifed the protocol type. On the server side you will receive a JSONObject:

public void handleRequest(String cmd, JSONObject jso, User u, int fromRoom)
{
	String str = jso.getString("str");
	int num = jso.getInt("num");
}

Recipe #2:

A slightly more complex example showing how to deal with nested objects. This time we also have an array of numbers.

var obj:Object = {}
obj.str = "Hello World!"
obj.num = 54321
obj.arr = [1,2,3,4,5]

sfs.sendXtMessage("test", "cmd", obj, "json")

On the server side you will receive a JSONObject instance, called jso:

public void handleRequest(String cmd, JSONObject jso, User u, int fromRoom)
{
	String str = jso.getString("str");
	int num = jso.getInt("num");
	JSONArray arr = jso.getJSONArray("arr");

	// Cycle through all items
	for (int i = 0; i < arr.length(); i++)
	{
		System.out.println("Item " + i + " = " + arr.getInt(i));
	}
}

Recipe #3:

This example shows how to create a more complex JSONObject. We want the client to receive an AS object like this:

var obj:Object = {}
obj.name = "King Arthur"
obj.from = "Camelot"
obj.age = 36
obj.roundTable = true
obj.inventory = {
		weapons: ["knife", "sword", "long sword"],
		food:["apple", "cheese", "bread", "wine"],
		garments:["boots", "armor", "helmet"]
		}

Here's how we can create the object in Java:

JSONObject jso = new JSONObject();
jso.putString("name", "King Arthur");
jso.putString("from", "Camelot");
jso.putInt("age", 36);
jso.putBoolean("roundTable", true);

// Create the inventory object 
JSONObject jsoInventory = new JSONObject();

// Weapons array
JSONArray jsaWeapons = new JSONArray();
jsaWeapons.put("knife");
jsaWeapons.put("sword");
jsaWeapons.put("long sword");

// Food array
JSONArray jsaFood = new JSONArray();
jsaFood.put("apple");
jsaFood.put("cheese");
jsaFood.put("bread");
jsaFood.put("wine");

// Garments array
JSONArray jsaGarments = new JSONArray();
jsaGarments.put("boots");
jsaGarments.put("armor");
jsaGarments.put("helmet");

// Add objects to inventory
jsoInventory.put("weapons", aoWeapons)
jsoInventory.put("food", aoFood)
jsoInventory.put("garments", aoGarments)

// Add the invetory object to the main ActionscriptObject
jso.put("inventory", aoInventory);

 


doc index