示例:并采用了amf二进制压缩的传输方式。
flex端代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="fun()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private var socket:Socket = new Socket();
private function fun():void{
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:Object = bytes.readObject();
msg += obj.n+":";
msg += obj.keys;
}
result.text += msg;
}
}
function send(){
var txt:String = msg.text;
socket.writeMultiByte(txt+"/n","gbk");
socket.flush();
msg.text="";
}
]]>
</fx: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()"/>
</s:Application>
服务器端代码:
主服务器类:
package yaner.mytest;
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 yaner2.mytest.AmfTool;
public class AmfSocket {
private ServerSocket socket;
private Socket client;
private Scanner in;
private PrintStream out;
private AmfTool tool = new AmfTool();
String line;
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()) {
line = in.nextLine();
if (line.trim().toLowerCase().equals("exit")) {
done = true;
} else {
out();
}
} else {
done = true;
}
}
if (done) {
client.close();
}
}
}
private void out() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("n", "value");
map.put("keys", line);
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();
}
}
}
amf转换类:
package yaner.mytest;
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;
}
}