转自: http://www.eb163.com/home/space-7286-do-blog-id-21.html
首先我希望大家看一看http://blog.csdn.net/ripotala/archive/2008/10/14/3075456.aspx这个地址
的文章对sgs有一定的了解先。
心得:(客户端篇)
在sgs官网上已经有人写了多套针对as3.0的库,对比觉得还是alienos比较好。具体下载方
法如下:首先登陆http://www.eb163.com/home/link.php?url=http://code.google.com%2Fp%2Fdarkstar-as3%2F下载这个库。这个分别是对应着是
HelloUser 和 HelloChannel 这两个sgs自带服务器例子。
跟着我们在使用之前请在com.alienos.sgs.as3.client.SimpleClient
的文章对sgs有一定的了解先。
心得:(客户端篇)
在sgs官网上已经有人写了多套针对as3.0的库,对比觉得还是alienos比较好。具体下载方
法如下:首先登陆http://www.eb163.com/home/link.php?url=http://code.google.com%2Fp%2Fdarkstar-as3%2F下载这个库。这个分别是对应着是
HelloUser 和 HelloChannel 这两个sgs自带服务器例子。
跟着我们在使用之前请在com.alienos.sgs.as3.client.SimpleClient
public function SimpleClient(host:String, port:int)
{
this.host = host;
this.port = port;
sock.addEventListener(Event.CLOSE, onClose);
sock.addEventListener(Event.CONNECT, onConnect);
sock.addEventListener(ProgressEvent.SOCKET_DATA, onData);
sock.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
messageFilter = new MessageFilter();
messageFilter.addEventListener(SgsEvent.RAW_MESSAGE, onRawMessage);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
//trace("securityErrorHandler: " + event);
dispatchEvent(new SgsEvent(SgsEvent.SECURITYERROR)); }
private function ioErrorHandler(event:IOErrorEvent):void {
//trace("ioErrorHandler: " + event);
dispatchEvent(new SgsEvent(SgsEvent.IOERROR)); }
{
this.host = host;
this.port = port;
sock.addEventListener(Event.CLOSE, onClose);
sock.addEventListener(Event.CONNECT, onConnect);
sock.addEventListener(ProgressEvent.SOCKET_DATA, onData);
sock.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
messageFilter = new MessageFilter();
messageFilter.addEventListener(SgsEvent.RAW_MESSAGE, onRawMessage);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
//trace("securityErrorHandler: " + event);
dispatchEvent(new SgsEvent(SgsEvent.SECURITYERROR)); }
private function ioErrorHandler(event:IOErrorEvent):void {
//trace("ioErrorHandler: " + event);
dispatchEvent(new SgsEvent(SgsEvent.IOERROR)); }
和com.alienos.sgs.as3.client.SgsEvent
public static const CHANNEL_MESSAGE:String = "channelMessage";
public static const CHANNEL_LEAVE:String = "channelLeave";
public static const RAW_MESSAGE:String = "rawMessage";
public static const IOERROR:String = "ioError";
public static const SECURITYERROR:String = "securityError";
加上以上的红色的字体不然会报io错误。
接着我们就可以使用这个库自带的main.mxml文件了,只要销加修改就能加入到你的项目里了。
心得:(服务端篇)
具体的请看英文原版ServerAppTutorial.pdf。
有的人问“偶当初登录是有传password给服务端的啊,为啥找遍API就是没有getpassword()的语句的呢?”
原来sgs是要你自定义登陆截取的。
比如你要检查密码是否正确:
你就要:XXXXXXX.properties上加上
com.sun.sgs.app.authenticators=自定义类
具体例子如下
HelloWorld.properties上加上
com.sun.sgs.app.name=HelloWorld
com.sun.sgs.app.root=data/HelloWorld
com.sun.sgs.app.port=1139
com.sun.sgs.app.listener=com.sun.sgs.tutorial.server.lesson1.HelloWorld
com.sun.sgs.app.authenticators=com.sun.sgs.server.auth.CustomAuthenticator
具体类如下:
package com.sun.sgs.server.auth;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.CredentialException;
import javax.security.auth.login.LoginException;
import com.sun.sgs.auth.Identity;
import com.sun.sgs.auth.IdentityAuthenticator;
import com.sun.sgs.auth.IdentityCredentials;
import com.sun.sgs.impl.auth.IdentityImpl;
import com.sun.sgs.impl.auth.NamePasswordCredentials;
public class CustomAuthenticator implements IdentityAuthenticator
{
private static final Logger logger =
Logger.getLogger(CustomAuthenticator.class.getName());
public CustomAuthenticator(Properties properties)
{
logger.info(">>> Custom Authenticator created! <<<");
}
public Identity authenticateIdentity(IdentityCredentials credentials)
throws LoginException
{
if(!credentials.getCredentialsType().equals(NamePasswordCredentials.TYPE_IDENTIFIER))
{
logger.warning("Unsupported credentials type!");
throw new CredentialException("Unsupported credentials type.");
}
NamePasswordCredentials npc = (NamePasswordCredentials)credentials;
String name = npc.getName();
char[] password = npc.getPassword();//获得password
logger.log(Level.INFO, ">>> Name: {0} Password: {1} <<<",
new Object[]{name, new String(password)});
if(password.length == 0 || password[0] != 'a')
{
logger.warning("Invalid password.");
throw new CredentialException("Invalid password.");
}
logger.log(Level.INFO, ">>> User {0} authenicated. <<<", new Object[]{name});
return new IdentityImpl(name);
}
public String[] getSupportedCredentialTypes()
{
// return new String[]{NamePasswordCredentials.TYPE_IDENTIFIER};
return new String[]{"NameAndPasswordCredentials"};
}
}