SGS的一点初级心得

转自: 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
复制代码
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));   }
    
复制代码


和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文件了,只要销加修改就能加入到你的项目里了。

心得:(服务端篇)
具体的请看英文原版ServerAppTuto rial.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 " };
        }
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值