Thursday, 18 November 2010

Setting up SmartFox server-side extensions

SmartFoxServer provides a client API (we'll be using the AS3 API) and a set of classes to create server-side extensions in Java. The server is set up with a series of zones, each of which acts as a separate application. A client is configured to connect to a particular zone by name. The details of which IP address, port and which zone are all kept in an XML file, and read by the client code the first time the client is asked to connect to the server. 

The diagram below is a really crude picture of what happens when the client sends a request to the server. 

The client packages up the name of the request it's trying to call, and the parameters needed for that call. Then it effectively lobs them over to the server in a bundle, along with the information about which zone it is supposed to be talking to. The server then uses the zone set up information to call the extension, which is set up with a list of RequestHandler classes. It uses the name request name passed through from the client to work out which RequestHandler class should be called, and passes the parameter data through to that class. 

That request handler can then use as much or as little extra backend code as you like, and optionally passes a response back to the client. The client can be a single user object (which can either be the generic SFS user object or a custom one that extends that object), or a list of users to alert. The client needs to use the events supplied with the client API to be notified of any responses, as the connection is asynchronous. 

Setting up the extension for the zone is pretty straightforward. Using the inbuilt Zone Configurator (is that really a word?), go to the tab labelled 'Zone extension'. The name field contains the folder that the extension jar is in within the extensions folder on the server. The type field defaults to Java, which is what we're using. And the file is the class within your jar (package included) that extends the SFSExtension class.

I haven't tried adding Room Extensions yet, but I suspect that will work along the same lines. 

There are other features that might become handy, such as multi-handlers which would allow us to break our code down into smaller chunks - ie. a list of requests for each area in a separate handler, rather than a long list of request handlers for everything in the first class. If/when we use them I'll revisit!

No comments:

Post a Comment