锁定老帖子 主题:FLASH与服务器通讯 (JAVA)
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-03-20
1.FLASH 通过URL地址获得服务器数据。这种方式最为简单,就像在浏览器的地址栏里面敲一样。 下面这个是载入一个图片,html页面也用同样方法载入。 //worldterminator原创,转载请注明此处 public function Net() { var loader:URLLoader; var request:URLRequest = new URLRequest("http://www.adobe.com/devnet/images/248x148/fldc_samples_3.jpg"); loader = new URLLoader(request); loader.addEventListener(Event.COMPLETE,completeListener); } function completeListener(event:Event):void { trace( "load completed "+loader.data+" are the data "); }
这种方法的缺点显而易见,大量的参数必须写到地址里,数据被暴露而且格式转换麻烦。 //worldterminator原创,转载请注明此处 2.通过AMF协议来通讯。AMF协议是ADOBE自己开发的一种建立在HTTP基础上的协议。可以装一个flash mx remoting来实现或者装openAMF来实现,这里只讨论openAMF。先下载一个openAMF,最好带example版本的。然后把openamf.ear放到tomcat的webapps下,启动tomcat,输入 如果看到空白页,说明openAMF好用了。如何用呢?
package abc; public class HelloServlet { public String hello() { return "this is java server!"; } }
用javac编译,把生成的class放到openAMF/classes/abc/
用javac编译,把生成的class放到openAMF/classes/abc/ FLASH端这样写: 建立一个netconnection,然后连接那个gateway,用call的方法调用java类的方法,格式是 包名.类名.方法名。至于返回值,用responder来接收,responder在接收到数据时会调用fun函数,把返回值写到fun函数的参数里。
3.socket方式通信这种方法应该可以和任何语言通信,使用TCP/IP协议。但是,对于FLASH PLAYER 9及其以后的版本,比较麻烦的一点就是安全设置,
socket = new Socket(); socket.addEventListener(ProgressEvent.SOCKET_DATA,receiveData); socket.addEventListener(Event.CONNECT,begin); socket.connect("127.0.0.1",843); function begin(event:Event):void { var s:String = "a\n"; socket.writeByte(s.charCodeAt(0)); socket.writeByte(s.charCodeAt(1)); socket.flush(); } FLASH端比较简单,先connect,添加CONNECT事件,如果连上了,执行begin函数,会向服务器写两个byte。
public class Main { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(843); File x; Thread receiver; System.out.println("server start"); Sender sender = new Sender(); while (true) { Socket socket=null; System.out.println("waiting..."); socket = ss.accept(); receiver = new Receiver(socket,sender); System.out.println("connect from address: "+socket.getInetAddress()); receiver.start(); } //System.out.println("server over"); } } //worldterminator原创,转载请注明此处 这个是非主线程运行的代码:
package for_flash_socket; import java.net.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; public class Receiver extends Thread { Socket socket; final int BYTE_ARRAY_MAX_LEN = 1000; final String FLASH_SAFE = "<policy-file-request/>"; static String safe = "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain- policy>\0"; Sender sender; public Receiver(Socket socket,Sender sender ) { this.socket = socket; this.sender = sender; } @Override public void run() { try { InputStream is = socket.getInputStream(); byte [] reader = new byte [BYTE_ARRAY_MAX_LEN]; BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())); OutputStream out = socket.getOutputStream(); sender.addOutput(out); int ch=0; String s; while ( is.read(reader) != -1 ) { //System.out.println("ch - "+ch); s = new String(reader); System.out.println("reader len:"+reader.length); System.out.println(s); if ( s!=null && s.indexOf("<policy-file-request/>")>=0 )//安全策略 { for ( int i=0;i<s.length();i++ ) { System.out.println(i+" "+(int)s.charAt(i)); } bw.write(safe,0,safe.length()); bw.flush(); //bw.close(); System.out.println(safe); } else { synchronized(sender) { sender.send(s); } } reader = new byte [BYTE_ARRAY_MAX_LEN]; } bw.close(); synchronized (sender) { sender.removeOutput(out); } } catch (Exception ex) { System.out.println("error in receiver"); ex.printStackTrace(); } try { socket.close(); System.out.println(socket.getInetAddress()+" disconnected"); } catch (IOException ex1) { System.out.println("socket close error"); Logger.getLogger(Receiver.class.getName()).log(Level.SEVERE, null, ex1); } } } //worldterminator原创,转载请注明此处 sender:
package for_flash_socket; import java.util.*; import java.io.*; public class Sender { ArrayList outputArrayList; public Sender() { outputArrayList = new ArrayList(); } public void addOutput(OutputStream out) { outputArrayList.add(out); } public void removeOutput(OutputStream out) { outputArrayList.remove(out); } public void send(String s) { for (int i=0;i<outputArrayList.size();i++) { PrintWriter pw = new PrintWriter((OutputStream)outputArrayList.get(i)); pw.write(s); pw.flush(); } } }
//worldterminator原创,转载请注明此处 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 4343 次