we've been quite busy in the last weeks working on final parts of the SmartFoxServer PRO engine. During the next weeks we'll be completing the various tests and writing new documentation.
Since we're constantly receiving questions about how server side extensions will work, I am giving you a quick example.
Let's say we want to send a list of numbers from the client to one of our custom extensions. The extension will simply return another list with those numbers multiplied by 2.
1) Here's the code for the client:
Code: Select all
public function sendValues()
{
var obj:Object = {}
obj.values = [100, 200, 300, 400, 500]
smartfox.sendXtMessage("simpleTest", "double", obj)
}
Just 3 lines: first we setup an object that will contain all the parameters we're sending to the extension.
We create an array property called "values" containing five numbers and finally we send the request to the extension using the sendXtMessage command.
The first argument is the name of the extension, the second is the name of the command we'd like to execute on that extension and finally we pass an object with the data to be processed.
2) Ok, now let'see how the request is received on the server side:
Code: Select all
function handleRequest(cmd, params, user, fromRoom)
{
if (cmd == "double")
{
var response = {}
response._cmd = "double"
response.values = []
for (var i = 0; i < params.values.length; i++)
{
var n = params.values[i]
response.values[i] = n * 2
}
_server.sendResponse(response, -1, null, [user])
}
}
A little bit more of code here because we're doing more things together: receiving the data, elaborate it and send a response back to the client.
The cmd argument is the name of the command requested from the client, in this case we've called it "double".
In the following lines we prepare the response object. Basically it is made of two fields only:
_cmd = the name of the command that was sent from the server (can be a good practice to keep it the same of the client-to-server request)
values = a list with the new values multiplied by 2
The for loop cycles through the list sent by the client and stores the doubled value in the response.value list.
Finally it's time to send the new data to the client using the _server.sendResponse() method.
The first argument is the response object, the second is the room number (-1, if we don't need this particular info), the third is the sender user object (null, if we don't need it) and the 4th parameter is a list with the users that will receive this message. In this case we just pass a list with the user that sent the request.
3) The third step is to see how the client handles the message fromt the server.
Here's the code:
Code: Select all
smartfox.onExtensionResponse = function(resObj:Object, type:String)
{
if (resObj._cmd == "double")
{
trace("res: " + resObj.values)
}
}
The code here is almost self-explaining. The resObj passed to the event handler contains the same properties we sent from the server, so its just a matter of reading them.
As a convention the "_cmd" property is used to pass the name of the command being sent.
That's all for now. In the next days I'll post some more example code bits showing database interaction!
Stay tuned for more news!