我正在尝试使用以下代码在Actionscript中构建一个简单的聊天程序。不幸的是,并非所有内容似乎都正确传输,因为它在前几个数据包之后没有收到任何数据包(因此不会收到所有文本更新)。我使用Wireshark看到所有的数据包都被传输,所以我不确定为什么Flash没有处理所有数据包。这是我的代码:
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Canvas id="login">
<mx:TextInput id="host" text="Host name"></mx:TextInput>
<mx:TextInput id="pswd" text="Key 1" keyDown="changePSWD()" y="30"></mx:TextInput>
<mx:TextInput id="key" text="Key 2" keyDown="changeKey()" y="60"></mx:TextInput>
<mx:TextInput id="port" text="Port" y="90"></mx:TextInput>
<s:Button label="Connect" y="90" x="150" click="doConnect()"></s:Button><s:Button y="120" label="Listen for connections (Don't worry. We'll automatically reconfigure your router)" click="listen()"></s:Button>
</mx:Canvas>
<mx:Canvas id="chatScreen" visible="false">
<s:TextArea id="mainchat" keyUp="sendTxt()" width="500" height="500"></s:TextArea>
</mx:Canvas>
<fx:Script>
<![CDATA[
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.net.Socket;
import flash.system.Security;
import flash.utils.ByteArray;
private var nativeSocket:Socket = new Socket();
private function init():void {
Security.allowDomain("*");
nativeSocket.addEventListener(Event.CONNECT, connected);
nativeSocket.connect("localhost", 337);
}
private function sendTxt():void {
//Send update signal
var buffer:ByteArray = new ByteArray();
//Signal type
buffer.writeByte(0);
//Signal data
buffer.writeUTF(mainchat.text);
buffer.position = 0;
securesocket.Write(buffer);
securesocket.Flush();
}
private var securesocket:NativeSocket;
private function changeKey():void {
key.displayAsPassword = true;
}
private function changePSWD():void {
pswd.displayAsPassword = true;
}
private function fail():void {
ExternalInterface.call("alert", "Failed to establish a connection to the remote host.");
}
private function success(socket:NativeSocket):void {
login.visible = false;
chatScreen.visible = true;
securesocket = socket;
securesocket.onRecv = parseData;
securesocket.ReadAsync(4086);
}
private function doConnect():void {
nativeInterface.openSocket(host.text, int(port.text), pswd.text, key.text, success, fail);
}
private function parseData(buffer:ByteArray, sender:NativeSocket):void {
try {
buffer.position = 0;
var opcode:int = buffer.readByte();
if (opcode == 0) {
mainchat.text = buffer.readUTF();
}
}catch (Exception:Error) {
}
securesocket.ReadAsync(9077);
}
private function clientConnectedToServer(endpoint:String, socket:NativeSocket):void {
try {
ExternalInterface.call("alert", endpoint + " has connected to Chat. The server listening socket will now close and you may begin your secure chat session.");
}catch (Exception:Error) {
}
mainserv.Stop();
//Begin chat
securesocket = socket;
securesocket.onRecv = parseData;
securesocket.ReadAsync(999999);
chatScreen.visible = true;
}
private var mainserv:server;
private function serverConnected(socket:server):void {
socket.clientConnect = clientConnectedToServer;
mainserv = socket;
login.visible = false;
}
private function listen():void {
nativeInterface.listenSocket(int(port.text), "xChat-server" + port.text, pswd.text, key.text, serverConnected);
}
private var nativeInterface:NativeInterface;
private function connected(evt:Event):void {
nativeInterface = new NativeInterface(nativeSocket);
}
]]>
</fx:Script>
答案 0 :(得分:1)
实施跨域策略后,Flash / Flex应用程序正确通信
您在端口#843上与之通信的文件
查看Cross-domain Policy File Specifications
这是一个示例策略文件:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*"/>
<allow-http-request-headers-from domain="*" headers="SOAPAction"/>
</cross-domain-policy>