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 ActionscriptObject
The ActionscriptObject class provides an easy way to manipulate Actionscript data coming from the client as well as sending data from the server side. You can learn about all the methods of this class in the javadoc. The followning is a selection of recipes to use ActionscriptObject.
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)
You will receive an ActionscriptObject:
public void handleRequest(String cmd, ActionscriptObject ao, User u, int fromRoom) { String str = ao.getString("str"); int num = (int) ao.getNumber("num"); }
Please note that numbers in ActionscriptObjects are typed as double, and you should cast them to other formats if you need it.
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)
On the server side you will receive an ActionscriptObject instance, called ao:
public void handleRequest(String cmd, ActionscriptObject ao, User u, int fromRoom) { String str = ao.getString("str"); int num = (int) ao.getNumber("num"); ActionscriptObject arr = ao.getObj("arr"); // Cycle through all items for (int i = 0; i < arr.size(); i++) { System.out.println("Item " + i + " = " + arr.getNumber(i)); } }
This example shows how to create an Actionscript object to send to the client. 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:
ActionscriptObject ao = new ActionscriptObject(); ao.put("name", "King Arthur"); ao.put("from", "Camelot"); ao.putNumber("age", 36); ao.putBool("roundTable", true); // Create the inventory object ActionscriptObject aoInventory = new ActionscriptObject(); // Weapons array ActionscriptObject aoWeapons = new ActionscriptObject(); aoWeapons.put(0, "knife"); aoWeapons.put(1, "sword"); aoWeapons.put(2, "long sword"); // Food array ActionscriptObject aoFood = new ActionscriptObject(); aoFood.put(0, "apple"); aoFood.put(1, "cheese"); aoFood.put(2, "bread"); aoFood.put(3, "wine"); // Garments array ActionscriptObject aoGarments = new ActionscriptObject(); aoGarments.put(0, "boots"); aoGarments.put(1, "armor"); aoGarments.put(2, "helmet"); // Add objects to inventory aoInventory.put("weapons", aoWeapons) aoInventory.put("food", aoFood) aoInventory.put("garments", aoGarments) // Add the invetory object to the main ActionscriptObject ao.put("inventory", aoInventory);
No comments:
Post a Comment