Ralf Rottmann wrote:
>
> In another area of the application I want to call it like this:
>
> response = SendCommand("login name");
> Alert.show(response),
>
Welcome to the world of asynchronous socket programming. In short, you
can't do what you want to do in the manner in which you have it coded
above. There is no way to make the Flash Player block until the socket
returns a value, so you're only choice is to move everything into event
handlers.
Essentially, this was the biggest hurdle for me in developing FlashVNC.
I was so used to doing thing synchronously that having to deal with
event handlers and callbacks for socket data threw a monkey wrench into
my master plan. The main issues I ran into:
1. Not sure which "socket write" the onSocketData event handler is
triggered by (if any).
2. Not enough data in the socket to finish processing (i.e. you need to
call 4 readBytes but only 3 bytes are available, so you get an EOFError
when trying to read the 4th byte and your program dies).
To fix #1, a simple solution is to use a state variable:
// send the login command
state = SENT_LOGIN;
SendCommand( "login name" );
// event handler
private function onSocketData( pe:ProgressEvent ):Void {
switch ( state ) {
// recevied data back after the login was sent, handle the response
case SENT_LOGIN: processLoginResponse( pe ); break;
case OTHER_STATE: processSomeOtherState( pe ); break;
}
}
Somewhere down the line, update the state and write some more data in
the socket so when data is received as a response you can call the
appropriate handler in the switch. This isn't quite100% how I'd
implement it, but you get the idea.
#2 above is potentially a LOT harder to handle. The gist of it is you
need to know how many bytes of data you need in the socket before
starting to read the data in the socket. That is, the first line in the
method body should be:
// method called from within onSocketData
private function processLoginResponse( pe:ProgressEvent ):Void {
if ( socket.bytesAvailable >= BYTES_NEEDED ) {
// have enough data, ok to process
} // else not enough data, so just wait for onSocketData to be
called again when more data arrives
}
The trick is knowing how many bytes are needed. After talking with
Spike about this - the best thing you can do is send the number of bytes
in the response as the first element in the response. That way the
client can read how many bytes the server sent, see how many bytes are
in the socket, and only process if all of the data has reached the client.
If you want an example of an application built with asynchronous
sockets, I recommend purchasing IFBIN's Flex By Example and looking at
the FlashVNC source code.
Also, Ely and Sho at Macromedia have some ideas on how to write "easier
to read" code for dealing with asynchronous events. Maybe they'll
contribute to this thread?
-d
------------------------ Yahoo! Groups Sponsor --------------------~-->
Most low income households are not online. Help bridge the digital divide today!
http://us.click.yahoo.com/I258zB/QnQLAA/TtwFAA/nhFolB/TM
--------------------------------------------------------------------~->
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/