Sun公司开源游戏服务器Project Darkstar Server——(Sun game server , 简称 sgs)学习笔记(六):服务器端HelloWorld
一、sgs Eclipse项目的建立
1、下载最新的sgs-server-dist-0.9.11,解压后放在C:\Sun\sgs_server\0.9.11。
2、在C:\Sun\sgs_server\0.9.11下建立项目文件夹webApp。
3、在webApp下建立eclipse项目test01
4、在test01下建立lib,conf文件夹
5、将C:\Sun\sgs_server\0.9.11\lib下的所有的文件复制到test01\lib文件下。
6、将test01\lib下的所有的jar添加到build path
编写HelloWorld
所有的PDS 应用都是从AppListener 开始的。一个AppListener 是一个操作应用启动和客户端登陆事件的对象。一个应用程序监听器是一个简单实现了AppListener 接口的类。因为一个AppListener 也是一个管理对象,它也需要实现Serializable 序列化标记接口。
就如上面提到的,AppListener 有两个方法:初始化和登陆。这个初始化方法会在应用启动时并且这个对象在对象仓库中是空的时候被调用。AppListener 是在应用第一次由系统自动创建的。这也就意味着每个应用都会被创建一次,如果用于该应用的对象仓库被删除,系统会返回一个“never having run this application(从未启动过该应用)”的状态。
import java.io.Serializable;
import java.util.Properties;
import com.sun.sgs.app.AppListener;
import com.sun.sgs.app.ClientSession;
import com.sun.sgs.app.ClientSessionListener;
/**
* Hello World example for the Project Darkstar Server.
* Prints {@code "Hello World!"} to the console the first time it is started.
*/
public class HelloWorld
implements AppListener, // to get called during application startup.
Serializable // since all AppListeners are ManagedObjects.
{
/** The version of the serialized form of this class. */
private static final long serialVersionUID = 1L;
/**
* {@inheritDoc}
* <p>
* Prints our well-known greeting during application startup.
*/
public void initialize(Properties props) {
System.out.println("Hello World!");
}
/**
* {@inheritDoc}
* <p>
* Prevents client logins by returning {@code null}.
*/
public ClientSessionListener loggedIn(ClientSession session) {
return null;
}
}
添加配置文件到test01\conf下
HelloWorld.boot
# example from the Project Darkstar Server Application Tutorial
SGS_DEPLOY=${SGS_HOME}/webApp/test01
SGS_PROPERTIES=${SGS_HOME}/webApp/test01/conf/HelloWorld.properties
SGS_LOGGING=${SGS_HOME}/webApp/test01/conf/logging.properties
HelloWorld.properties
# example from the Project Darkstar Server Application Tutorial
com.sun.sgs.app.name=HelloWorld
com.sun.sgs.app.root=#$%@#data/HelloWorld
com.sun.sgs.impl.transport.tcp.listen.port=1139
com.sun.sgs.app.listener=com.papaya.HelloWord
logging.properties
# logging facility.
# The system will look for this config file, first using
# a System property specified at startup:
#
# >java -Djava.util.logging.config.file=myLoggingConfigFilePath
#
# If this property is not specified, then the config file is
# retrieved from its default location at:
#
# JDK_HOME/jre/lib/logging.properties
# Global logging properties.
# ------------------------------------------
# The set of handlers to be loaded upon startup.
# Comma-separated list of class names.
# (? LogManager docs say no comma here, but JDK example has comma.)
# handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
handlers=java.util.logging.ConsoleHandler
# Default global logging level.
# Loggers and Handlers may override this level
.level=INFO
#.level=ALL
# Loggers
# ------------------------------------------
# Loggers are usually attached to packages.
# Here, the level for each package is specified.
# The global level is used by default, so levels
# specified here simply act as an override.
# myapp.ui.level=ALL
# myapp.business.level=CONFIG
# myapp.data.level=SEVERE
# Handlers
# -----------------------------------------
# --- ConsoleHandler ---
# Override of global logging level
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# --- FileHandler ---
# Override of global logging level
java.util.logging.FileHandler.level=ALL
# Naming style for the output file:
# (The output file is placed in the directory
# defined by the "user.home" System property.)
java.util.logging.FileHandler.pattern=%h/java%u.log
# Limiting size of output file in bytes:
java.util.logging.FileHandler.limit=50000
# Number of output files to cycle through, by appending an
# integer to the base file name:
java.util.logging.FileHandler.count=1
# Style of output (Simple or XML):
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
运行HelloWorld
1、在C:\Sun\sgs_server\0.9.11\webApp\test01\bin下添加META-INF文件夹
2、在META-INF下添加配置文件:app.properties
# example from the Project Darkstar Server Application Tutorial
com.sun.sgs.app.name=HelloWorld
com.sun.sgs.app.listener=com.papaya.HelloWord
com.sun.sgs.impl.transport.tcp.listen.port=1139
3、对HelloWorld进行打包,将打包后的jar文件复制到C:\Sun\sgs_server\0.9.11\webApp\test01目录下
4、在C:\Sun\sgs_server\0.9.11下编写bat文件
运行.bat
5、运行“运行.bat”文件。就会看到效果了