[AS3.0]一步一步学ActionScript 3.0(十七)

今天我们来看看JAVA 的remoting --- OPENAMF,这个也是我用的比较多的一种,先下载openamf-1.0RC12.zip文件,解压后找到openamf.war文件,我们来看看如果配置OPENAMF. 

1、这们这里用的JAVA服务器是Tomcat,将openamf.war复制到Tomcat的webapps目录里,重启Tomcat,我们会发现多了一个文件夹openamf。
2、在webapps里面在建一个文件夹,比如取名叫flashService,然后将openamf里面的WEB-INF复制到flashService里面。
3、在我们的浏览器里输入http://localhost:8080/flashService/gateway,测试一下openamf是否配置好了,如果配置正确,那么会显示一个空白页面,什么都没有显示。但我这里不行,报了一个404的错误,具体错误我也没找到原因,修改了一下WEB-INF里的web.xml文件。

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"
>


< web-app >
   
< display-name > flashserver </ display-name >
   
< description > java for flash application </ description >
    
    
<!--  openamf gateways  -->
    
< servlet >
        
< servlet-name > DefaultGateway </ servlet-name >
        
< display-name > DefaultGateway </ display-name >
        
< description > DefaultGateway </ description >
        
< servlet-class > org.openamf.DefaultGateway </ servlet-class >
        
< init-param >
            
< param-name > OPENAMF_CONFIG </ param-name >
            
< param-value > /WEB-INF/openamf-config.xml </ param-value >
            
< description >   </ description >
        
</ init-param >
    
</ servlet >
    
    
< servlet-mapping >
        
< servlet-name > DefaultGateway </ servlet-name >
        
< url-pattern > /gateway </ url-pattern >
    
</ servlet-mapping >
    
< session-config >
        
< session-timeout > 30 </ session-timeout >
    
</ session-config >
    
< welcome-file-list >
        
< welcome-file > index.html </ welcome-file >
        
< welcome-file > index.jsp </ welcome-file >
    
</ welcome-file-list >
</ web-app >

个人是用这个方法解决的,不知道还有没有其它的方法,在输入http://localhost:8080/flashService/gateway,发现是空白,那么我们这个JAVA remoting就配置好了

先写个简单的helloWorld,先看看java端:

package  net.smilecn.helloWorld;

public   class  Hello{

    
public  String sayHello(String name){
        
return   " hello!  " + name;
    }

}

这个代码比较简单,应该不难理解,将它编译,生成Hello.class文件,注意我们这个程序加了包,所以要把这个class文件放到,flashService/WEB-INF/classes/net/smilecn/helloWorld里面,classes是我们放class文件的地方,net/smilecn/helloWorld是因为程序有这个包,我们要建立这个路径,当然打包成jar就不用了

再来看flash端,建一个FLA文件,建一个文档类:

package  net.smilecn.remoting{
    
    
import  flash.display.Sprite;
    
import  flash.net.NetConnection;
    
import  flash.net.Responder;
    
    
public   class  MainForm  extends  Sprite{
        
        
private  var gateWayUrl:String  =   " http://localhost:8080/flashService/gateway " ;
        
private  var remotingNc:NetConnection;
        
        
public  function MainForm(): void {
            init();
        }
        
        
private  function init(): void {
            remotingNc 
=   new  NetConnection();
            remotingNc.connect(gateWayUrl);
            remotingNc.call(
" net.smilecn.helloWorld.Hello.sayHello " , new  Responder(getMsgResult,getMsgFault), " arrow " );
        }
        
        
private  function getMsgResult(re: * ){
            trace(
" 从remoting返回信息成功: " ,re);
        }
        
        
private  function getMsgFault(fe: * ){
            trace(
" 从remoting返回信息失败: " ,fe.code);
        }
    
    }
}

NetConnection是一个网络连接的类,remoting可以用这个,连接FMS也可以用这个(在后面我还会写一些FMS的教程),

connect方法是连接remoting服务器,其实就是我们之前测试的地址,call方法是调用java的方法,net.smilecn.helloWorld是包名,Hello是类名,sayHello是方法名,所以一般是包名+类名+方法名,如果没有包名,就不用加包名,getMsgResult是调用正确返回结果,re就是结果,getMsgFault是错误信息返回

如果调用正确,就会输出从remoting返回信息成功:hello!  arrow

这就是最基本的remoting使用,我们可以很方便的调用JAVA的方法,并返回一些值.

下节继续!

(本教程如需转载请注明出处!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值