Java扩展模板
介绍
本文讲用java进行服务器端扩展的模板。
»java 扩展的文件的结构
扩展文件由以下4部分组成:
1) Initialize 初始化
2) Handle client requests 处理客服端的请求
3) Handle internal server events 处理内部事件
4) Destroy 销毁
以上4个功能是通过以下4个java方法来完成的: init(), handleRequest(), handleInternalEvent(), destroy()
请注意我们重载了 handleRequest() 方法 来处理 XML based 和String based 信息。
»模板 (服务器版本< 1.5.0)
模板的代码:
import java.nio.channels.SocketChannel; import java.util.*; import it.gotoandplay.smartfoxserver.db.*; import it.gotoandplay.smartfoxserver.data.*; import it.gotoandplay.smartfoxserver.exceptions.*; import it.gotoandplay.smartfoxserver.extensions.*; import it.gotoandplay.smartfoxserver.lib.ActionscriptObject; import it.gotoandplay.smartfoxserver.events.InternalEventObject; public class ExtensionTemplate extends AbstractExtension { /** * Initializion point: * * this method is called as soon as the extension * is loaded in the server. * * You can add here all the initialization code */ public void init() { trace("Extension initialized"); } /** * This method is called by the server when an extension * is being removed / destroyed. * * Always make sure to release resources like setInterval(s) * open files etc in this method. * * In this case we delete the reference to the databaseManager */ public void destroy() { trace("Extension destroyed"); } /** * Handle Client Requests in XML format * * @param cmd the command name * @param ao the actionscript object with the request params * @param u the user who sent the request * @param fromRoom the roomId where the request was generated */ public void handleRequest(String cmd, ActionscriptObject ao, User u, int fromRoom) { // Your code here } /** * Handle Client Requests in String format * * @param cmd the command name * @param params an array of String parameters * @param u the user who sent the request * @param fromRoom the roomId where the request was generated */ public void handleRequest(String cmd, String params[], User u, int fromRoom) { // Your code here } /** * Handle Internal Server Events * * @param ieo the event object */ public void handleInternalEvent(InternalEventObject ieo) { // Your code here } }
»模板 (服务器版本 >= 1.5.0)
代码
import java.nio.channels.SocketChannel; import java.util.*; import it.gotoandplay.smartfoxserver.db.*; import it.gotoandplay.smartfoxserver.data.*; import it.gotoandplay.smartfoxserver.exceptions.*; import it.gotoandplay.smartfoxserver.extensions.*; import it.gotoandplay.smartfoxserver.lib.ActionscriptObject; import it.gotoandplay.smartfoxserver.events.InternalEventObject; import org.json.JSONObject; public class ExtensionTemplate extends AbstractExtension { /** * Initializion point: * * this method is called as soon as the extension * is loaded in the server. * * You can add here all the initialization code */ public void init() { trace("Extension initialized"); } /** * This method is called by the server when an extension * is being removed / destroyed. * * Always make sure to release resources like setInterval(s) * open files etc in this method. * * In this case we delete the reference to the databaseManager */ public void destroy() { trace("Extension destroyed"); } /** * Handle Client Requests in XML format * * @param cmd the command name * @param ao the actionscript object with the request params * @param u the user who sent the request * @param fromRoom the roomId where the request was generated */ public void handleRequest(String cmd, ActionscriptObject ao, User u, int fromRoom) { // Your code here } /** * Handle Client Requests in String format * * @param cmd the command name * @param params an array of String parameters * @param u the user who sent the request * @param fromRoom the roomId where the request was generated */ public void handleRequest(String cmd, String params[], User u, int fromRoom) { // Your code here } /** * Handle Client Requests in JSON format * * @param cmd the command name * @param params a JSONObject with the request parameters * @param u the user who sent the request * @param fromRoom the roomId where the request was generated */ public void handleRequest(String cmd, JSONObject jso, User u, int fromRoom) { // Your code here } /** * Handle Internal Server Events * * @param ieo the event object */ public void handleInternalEvent(InternalEventObject ieo) { // Your code here } }