Reputation: 105
The ServerSocketConnectEvent is not firing when I try to have two instances of the same app talk to each other. Here's my scenario:
I have an AS3 Adobe AIR app that communicates to another instance of it over localhost. Both apps listen on a port and try to connect to each others ports. That is, instance 1 listens on port 50000 and tries to connect to port 50001 on instance 2, which listens on port 50001 and tries to connect to port 50000. The apps try to connect to each other every 100ms until the connection is established. I used RawCap to capture the data on localhost, and I see interleaving of packets meant for both destination ports 50000 and 50001.
My setup consists of running the app from FlashDevelop in debug mode and running the bundled exe separately (I am able to have multiple instances of the app as per this: How to get ability to start up multiple same Adobe Air applications?). I do not see ServerSocketConnectEvent.CONNECT fire. Here is some relevant code:
// triggered by clicking on a sprite
public function onClickConnect(event:MouseEvent):void
{
m_connect.disable();
m_serverSocket = createServerSocket(int(m_localPortText.text));
m_remoteSocket = new Socket();
m_remoteSocket.addEventListener(Event.CONNECT, connectionMade);
m_remoteSocket.addEventListener( Event.CLOSE, connectionClosed );
m_remoteSocket.addEventListener( IOErrorEvent.IO_ERROR, socketFailure );
m_remoteSocket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityError );
//m_remoteSocket.addEventListener( ProgressEvent.SOCKET_DATA, dataReceived);
m_retryTimer = new Timer(100);
m_retryTimer.addEventListener(TimerEvent.TIMER, connectToRemote);
m_retryTimer.start();
}
// this retries connecting to the remote client
public function connectToRemote(event:TimerEvent):void
{
try
{
//Try to connect
m_remoteSocket.connect( "127.0.0.1", int(m_remotePortText.text));
}
catch( e:Error ) { trace( e ); }
}
public function createServerSocket(port:int):Socket
{
try
{
// Create the server socket
var serverSocket:ServerSocket = new ServerSocket();
// Add the event listener
serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, gotConnection );
//serverSocket.addEventListener( Event.CLOSE, onClose );
serverSocket.bind(port, "127.0.0.1" );
// Listen for connections
serverSocket.listen();
// this prints fine
trace("Listening on " + serverSocket.localAddress + ":" + serverSocket.localPort);
}
catch (e:Error)
{
trace(e);
}
return m_serverSocket;
}
// this is for connections from clients
// it never fires
public function gotConnection(event:ServerSocketConnectEvent):void
{
//The socket is provided by the event object
m_serverSocket = event.socket;
trace("Connected to client");
}
// this is for connecting to the other app
public function connectionMade(event:Event):void
{
trace(event);
m_retryTimer.stop();
}
public function socketFailure(event:IOErrorEvent):void
{
trace(event);
}
public function connectionClosed(event:Event):void
{
trace(event);
}
public function securityError(event:SecurityErrorEvent):void
{
trace(event);
}
Any ideas?
Thanks
Upvotes: 1
Views: 494
Reputation: 18193
It looks like your server socket is going out of scope and being garbage collected.
In the method createServerSocket()
you instantiate the server socket with the local variable serverSocket
. But that same method returns m_serverSocket which is null. I think you intended to return the local var serverSocket
:)
Upvotes: 1