SmartfoxServer服务端扩展逻辑处理模版

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
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值