Witam próbuję napisać aplikację klient - serwer. Klient we Flex-ie a serwer w PHP.
Kod klienta:
<?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"
width="100%"
height="100%"
applicationComplete="connectToServer(event)">
<fx:Declarations></fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import flash.events.DataEvent;
import flash.net.XMLSocket;
private function onData(event:DataEvent):void
{
buttonik.label = 'dziala';
//ExternalInterface.call("window.location.alert", 'xxx');
}
protected function connectToServer(event:FlexEvent):void
{
var host:String = '127.0.0.1';
var port:Number = 9999;
var socket:XMLSocket = new XMLSocket();
configureListeners(socket);
socket.connect(host, port);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CLOSE, closeHandler);
dispatcher.addEventListener(Event.CONNECT, connectHandler);
dispatcher.addEventListener(DataEvent.DATA, dataHandler);
}
private function closeHandler(event:Event):void {
buttonik.label = 'close';
}
private function connectHandler(event:Event):void {
buttonik.label = 'connect';
}
private function dataHandler(event:DataEvent):void {
buttonik.label = 'data'+ event.data;
}
]]>
</fx:Script>
<s:Button id="buttonik" label="" width="100" height="20"></s:Button>
</s:Application>
Kod serwera:
// Set the ip and port we will listen on
$address = 'localhost';
$port = 9999;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
while(true){
$client = socket_accept($sock);
if($client !== false){
echo 'Klient podlaczony'."\n";
$data = 'xxx';
echo 'Zapisano '. socket_write($client, $data, strlen($data)) .' bajty danych'."\n";
socket_close($client);
}
}
socket_close($sock);
Problem w tym jest, że socket_write zwraca faktyczną ilość ilość wysłanych danych ale Flex ich nie odbiera i funkcja dataHandler nie jest wykonywana. Wydaje się, że błąd jest gdzieś we Flex-ie ale nie jestem biegły we AS i nie wiem gdzie szukać błędu. Pomoże ktoś?
Dzięki