Hi all,
Hopefully someone can help. I’m trying to connect to a Java socket server on another pc on my network via a binary socket in AS3. I would like to be able to download an image from the java server and display it in a Flash CS3 movie. I have managed so far to get the socket to connect to my java server and download the raw byte data of the image.
What I am struggling with is getting the AS3 script to know that it is the end of the file and then display the image in the stage. I know that there is an Event.COMPLETE event for URL Loaders, but do not know how to implement this for a binary socket. You will see from my code that I have tried to send a byte array of “FIN” when the image has finished sending, but cannot get the client to recognise this. Anyone any ideas?
Here is my code so far:
var s:Socket = new Socket("PHOENIX", 31742);
var imgData:ByteArray = new ByteArray();
var imgStream:URLStream = new URLStream();
var loader:Loader = new Loader() ;
s.addEventListener( ProgressEvent.SOCKET_DATA , imageStreamProgress );
s.writeUTF("file");
s.flush();
trace("file requested");
function imageStreamProgress( event:Event ):void
{
// if there are no bytes do nothing
if( s.bytesAvailable == 0 ) return
// ooo bytes process the image data
trace(s.bytesAvailable);
this.processImageData();
}
function imageStreamComplete():void
{
// if connected, stop that.
if ( s.connected ) s.close();
// lets refresh the displayList after rendering cycle
//imageCanvas.callLater( this.processImageData );
trace("FIN");
trace(s.bytesAvailable);
}
function processImageData():void
{
// if connected, read all the bytes that have been loaded into the aggregate bytearray
if ( s.connected ) s.readBytes( imgData , imgData.length );
var test:String = imgData.toString();
trace (test);
if (test.indexOf("FIN") != 0) {
// clean out all the crud in that loader
loader.unload();
//push the aggregate bytearray of loaded image data in there.
loader.loadBytes( imgData );
} else {
imageStreamComplete();
}
}