Java与AS3.0通信

本人目前使用的AS3.0程序由于使用json数据,而客户端AS3.0内部又不支持json,而是提供的库来解析的,所以多少效率上还是有些影响,于是就想到用AMF3格式来传输,这样就免去了解析过程,不过服务器端多了个代码一方面开销会较大,另一方面速度也不一定很快[本人对flash remoting执行效率没有准确认识,所以这个只是个人感觉而已]。flash remoting mx本身就可以让java与flash通信.OpenAMF给的例子中都有#include "NetServices.as"#include "NetDebug.as"这两个东西据说就是flash remoting mx里头的。我个人对java不熟,所以还废了点周折。1、首先用eclipse建个工程,搞个在默认命名空间下的类Wangcheng.java,然后编译成.class文件。java类里头的内容是这样的:JAVA:public class Wangcheng { public String name ="wc-wangcheng"; public String Say(String str) { return str; }}2、写as文件然后编译。实际上as3.0中主要用来通信的是NetConnection类。ACTIONSCRIPT:package { import flash.display.Sprite; import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.events.Event; import flash.events.MouseEvent; import flash.net.NetConnection; import flash.net.ObjectEncoding; import flash.net.Responder; public class OpenAMF extends Sprite { public var txt:String=""; private var init:Boolean; public var tf:TextField=new TextField(); public var nc:NetConnection ; public function OpenAMF() { stage.addEventListener(MouseEvent.CLICK,click); this.Init(); } public function Init():void { tf.x=100; tf.y=100; tf.backgroundColor=0xffffff; tf.width=300; tf.height=200; this.addChild(tf); } private function click(event:Event ):void { nc= new NetConnection(); nc.client = nc; nc.objectEncoding = flash.net.ObjectEncoding.AMF3; nc.connect("http://localhost:8400/gateway"); tf.appendText("发送Say--事件"+"n"); nc.call("Wangcheng.Say",new Responder(SuccResult,OtherStatus),"wang_cheng_click"); tf.appendText("发完了 n"); } private function SuccResult(re:Object) :void{ tf.text=re+"n"+tf.text; tf.appendText("成功了 n"); } private function OtherStatus(re:Object):void { tf.appendText("出错了 n"); } }}3、文件放到服务器下。我直接把生成的OpenAMF.swf文件和引用OpenAMF.swf的OpenAMF.html文件防止到tomcat5.5的root文件夹下了,而便已得到的Wangcheng.class文件放到Tomcat 5.5webappsROOTWEB-INFclasses目录下。4、修改配置文件。OpenAMF给我们提供了几个配置文件,其中包括openamf-config.xml,build-webservice.xml和applicationContext-xml,将这3个文件都放到Tomcat 5.5webappsROOTWEB-INF目录下,并修改该目录下web.xml的内容,增加节点:XML:<?xml version="1.0" encoding="ISO-8859-1"?><!--Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <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>Location of the OpenAMF config file.</description> </init-param> </servlet> <servlet-mapping> <servlet-name>DefaultGateway</servlet-name> <url-pattern>/gateway</url-pattern> </servlet-mapping> </web-app>直接使用OpenAMF自己的web.xml配置文件有问题,我也没仔细看。5、另外还要把openamf.jar文件考到Tomcat 5.5webappsROOTWEB-INFlib目录下。然后启动tomecat就可以看到效果了。
需要的朋友也可以这里下载root.zip,替换掉tomcat5.0默认的root文件夹即可运行,注意,我使用的tomecat是在8400端口提供服务,一般是8080,如果端口不同可以修改端口后重新编译as文件。

评论: 0 | 引用: 0 | 查看次数: 28
发表评论
你没有权限发表留言!