Flexのsocketとjavaはamf 3を使用して通信する


amf 3,adobeが創設したデータフォーマットはバイナリを使用するためxmlなどの冗長な文字データ量が少なくなります!また、多くの試験報告でamf伝達効果が高いことが判明した.
今回はFlexのSocketクラスを使ってjavaのサービス側と通信し、
手順は次のとおりです.
クライアントはsocketクラスでjavaサーバに接続し、
クエリー文字列を使用して要求します.
JAvaはデータをhashmapにカプセル化し、最後にhashmapをamfoutputstreamでflexのクライアントに出力する
flexがデータを復号するとObject!
以下は詳細なコードです(ほほほ、記録を残します)
1.adobeのパッケージをjavaのプロジェクトにインポートします!
flex-messaging-core.jar
flex-messaging-common.jar
2.javaのsocketサーバを構築する
パッケージ構造:com.sk.socket
package com.sk.socket;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

import com.sk.amf.AmfTool;

public class AmfSocket {
private ServerSocket socket;
private Socket client;
private Scanner in;
private PrintStream out;
private AmfTool tool = new AmfTool();

public AmfSocket(int port) throws IOException {
socket = new ServerSocket(port);
}
public void start() throws IOException{
System.out.println("Amf      ");
while (true) {
client = socket.accept();
in = new Scanner(client.getInputStream());
out = new PrintStream(client.getOutputStream());
boolean done = false;
while (!done) {
if (in.hasNextLine()) {
String line = in.nextLine();
if (line.trim().toLowerCase().equals("exit")) {
done = true;
} else {
out();
}
} else {
done = true;
}
}
if(done){
client.close();
}
}
}

private void out() {
HashMap map = new HashMap();
map.put("n", "value");
map.put("keys", map);
try {
byte[] bytes = tool.compressOutput(map);
out.write(bytes);
System.out.println(Arrays.toString(bytes));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
new AmfSocket(8888).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

2.ツールクラスAmfTool、パッケージ構造:com.sk.amf、javaのhashmapを2進数に変更したデータをflexに出力することを目的とするツール
package com.sk.amf;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;

public class AmfTool {
private Deflater deflater = new Deflater();
private int cacheSize = 1024;
/**
*    ,    
* @return byte[]
* @throws IOException
*/
public byte[] compressOutput(Object obj) throws IOException{
Amf3Output amfOut = new Amf3Output(new SerializationContext());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutputStream);
amfOut.setOutputStream(dataOutStream);
amfOut.writeObject(obj);
amfOut.flush();
dataOutStream.flush();
byte[] msg = byteArrayOutputStream.toByteArray();
msg = compress(msg);
amfOut.close();
dataOutStream.close();
return msg;
}
/**
*     
* @param input
* @return
* @throws IOException
*/
private byte[] compress(byte[] input) throws IOException{
deflater.reset();
deflater.setInput(input);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[cacheSize];
int len;
while(!deflater.finished()){
len = deflater.deflate(buf);
outputStream.write(buf,0,len);
}
byte[] tmpByte = outputStream.toByteArray();
outputStream.close();
return tmpByte;
}
}

3.Flexのsocketクライアント
FlexBuilder 3でFlexプロジェクトを作成し、次にmxmlファイルを作成します.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="fun()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var socket:Socket = new Socket();
function fun(){
socket.connect("127.0.0.1",8888);
socket.addEventListener(Event.CONNECT,conn);
socket.addEventListener(ProgressEvent.SOCKET_DATA,showData);
socket.addEventListener(Event.CLOSE,disconn);
function conn(e){
Alert.show("conn");
}
function disconn(e){
Alert.show("disconn");
}
function showData(e){
var msg:String="";
while(socket.bytesAvailable){
var bytes:ByteArray = new ByteArray();
socket.readBytes(bytes,0,socket.bytesAvailable);
bytes.uncompress();
var obj = bytes.readObject();

msg += obj.n+":";
msg += obj.keys.n;
}
result.text += msg;
}
}
function send(){
var txt:String = msg.text;
socket.writeMultiByte(txt+"
","gbk"); socket.flush(); msg.text=""; } ]]> </mx:Script> <mx:TextArea x="10" y="10" width="356" height="325" id="result"/> <mx:TextArea x="10" y="369" width="282" id="msg"/> <mx:Button x="300" y="391" label="Send" click="send()"/> </mx:Application>

4.まずjavaのサービスを開始し、次にFlexクライアントを実行します...
注意:現在javaのHashmapだけがas 3と通信できるようです.Hashmapはas 3のObject,jsのjsonフォーマットと一致しているからです(key,value形式)