3
我希望通過ServerSocket類創建一個Air 2服務器(您將在下面的代碼中找到關於它的示例)。Air 2 ServerSocket跨域問題
約束:
- 服務器必須在空氣
- 客戶必須通過Web瀏覽器顯示
客戶端顯示與Web瀏覽器,所以當客戶想建立一個連接Air服務器,Flash通過套接字發送跨域請求,服務器將其發回,但沒有發生。
As3Doc指定當Flash發送跨域請求時,服務器必須將其發回,然後Flash關閉連接並在跨域確定時打開新連接。
我已經嘗試了不同的設置,但沒有任何工作,客戶端永遠不會收到CONNECTED的事件。
任何想法?
服務器端代碼:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">
<s:TextArea x="0" y="0" width="100%" height="100%" id="log"/>
<fx:Script>
<![CDATA[
private var _server : ServerSocket = new ServerSocket;
private function init() : void
{
_server.bind(4500, "127.0.0.1");
_server.addEventListener(ServerSocketConnectEvent.CONNECT, onClientConnection);
_server.listen();
}
private function onClientConnection(e : ServerSocketConnectEvent) : void
{
var socket : Socket = e.socket;
log.appendText("Client connected : " + socket.localAddress + ":" + socket.localPort + "\n");
socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
}
private function onData(e:Event) : void
{
var socket : Socket = e.target as Socket;
log.appendText("Data : " + socket.readUTFBytes(socket.bytesAvailable));
socket.writeUTF(
'<cross-domain-policy>' +
' <allow-access-from domain="*" to-ports="4500" />' +
'</cross-domain-policy>'
+ String.fromCharCode(0)
);
socket.writeByte(0);
socket.flush();
socket.close();
}
]]>
</fx:Script>
</s:WindowedApplication>
客戶端代碼:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.text.TextField;
import flash.utils.setTimeout;
public class TestClient extends Sprite
{
private var log : TextField;
private var _socket : Socket;
public function TestClient()
{
log = new TextField;
log.width = stage.stageWidth;
log.height = stage.stageHeight;
addChild(log);
_socket = new Socket;
_socket.addEventListener(Event.CONNECT, onConnection);
_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
_socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
tryConnection();
}
private function tryConnection() : void
{
log.appendText("Try connection ... \n");
_socket.connect("127.0.0.1", 4500);
}
private function onConnection(e : Event) : void
{
log.appendText("Connected !");
}
private function onError(e : Event) : void
{
log.appendText(e.toString() + "\n");
setTimeout(tryConnection, 1000);
}
}
}
我太親近了:'( – 2011-05-10 12:17:32