Reputation: 5529
I'm trying to use the following code to build a simple chat program in Actionscript. Unfortunately, not everything seems to be transmitted correctly, because it doesn't receive any packets after the first few packets (so not all of the text updates get received). I used Wireshark and saw that all of the packets were being transmitted, so I'm not sure why Flash isn't processing all of them. Here's my code:
<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>
Upvotes: 0
Views: 196
Reputation: 5968
Flash/Flex applications communicate correctly after implementing a cross-domain policy
file which you communicate with on port #843
Take a look at Cross-domain Policy File Specifications
This is a sample policy-file:
<?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>
Upvotes: 1