gpt4 book ai didi

actionscript-3 - Adobe AIR : ServerSocketConnectEvent not firing for localhost connections

转载 作者:可可西里 更新时间:2023-11-01 02:52:35 26 4
gpt4 key购买 nike

当我尝试让同一应用程序的两个实例相互通信时,ServerSocketConnectEvent 没有触发。这是我的场景:

我有一个 AS3 Adob​​e AIR 应用程序,它通过本地主机与它的另一个实例通信。两个应用程序都在一个端口上监听并尝试连接到彼此的端口。也就是说,实例 1 在端口 50000 上监听并尝试连接到实例 2 上的端口 50001,实例 2 在端口 50001 上监听并尝试连接到端口 50000。应用程序尝试每 100 毫秒相互连接一次,直到建立连接。我使用 RawCap 捕获了本地主机上的数据,我看到了目标端口 50000 和 50001 的数据包交错。

我的设置包括在 Debug模式下从 FlashDevelop 运行应用程序并单独运行捆绑的 exe(我能够拥有应用程序的多个实例,如下所示:How to get ability to start up multiple same Adobe Air applications?)。我没有看到 ServerSocketConnectEvent.CONNECT 火。这是一些相关代码:

// 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);
}

有什么想法吗?

谢谢

最佳答案

看起来您的服务器套接字超出范围并被垃圾收集。

在方法 createServerSocket() 中,您使用局部变量 serverSocket 实例化服务器套接字。但同样的方法返回 null 的 m_serverSocket。我想你打算返回本地 var serverSocket :)

关于actionscript-3 - Adobe AIR : ServerSocketConnectEvent not firing for localhost connections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com