关于Flash Player 10 socket connection timeout

转载请注明,来自:http://blog.csdn.net/skyman_2001

今天我在调试游戏的时候,发现一个有趣的现象,就是flash连接上服务器后,服务器关闭连接,flash会触发securityError事件,用google搜索了一下,找到了官方的这篇文章:http://helpx.adobe.com/flash/kb/flash-player-10-socket-connection.html,里面讲了这是Flash Player 10才改成这样的,摘录如下:

Issue

Adobe Flash Player 10 includes significant change to the way that socket timeout is handled, adding significant user control.

Connections that previously succeeded after a long delay (due to outages or network congestion) may now result in a securityError event with Flash Player 10.

Reason

In Flash Player 10, ActionScript Socket and XMLSocket objects, all securityError events will be sent after a predefined amount of time has elapsed since the call to connect(). The predetermined timeout is 20 seconds by default but can be specified by ActionScript developers using the new Socket.timeout and XMLSocket.timeout APIs. If the timeout elapses and no connection has been established, the connection attempt will be aborted and a securityError dispatched.

Note: This change affects SWF files of all versions played in Flash Player 10 and later. This security change can potentially affect any SWF file that uses the Socket or XMLSocket classes. This change affects all non-app content in Adobe AIR (however, AIR app content itself is unaffected).

Solution

Developers should be sure to account for the new error by listening for the SecurityErrorEvent.SECURITY_ERROR event from Socket and XMLSocket objects in their code. The best practice is to set explicit timeout for any socket content over which you have control.

Longer timeouts favor greater robustness under marginal network conditions at the expense of longer delays in discovering failures.
Shorter timeouts favor quicker discovery of failures at the expense of reduced robustness under marginal network conditions.

也就是Socket有个连接超时时间属性(timeout),指示建立连接时需等待的毫秒数,默认值是20秒。如果Socket已经连接上,但timeout时间到时发现连接已经断开了,就会触发securityError事件(例如服务器在timeout时间之内关闭连接,则timeout时间到时会触发securityError事件;但如果服务器是在timeout时间之外关闭连接,就不会触发securityError事件)。不过如果服务器监听端口根本就没打开,那么Flash Socket就连接不上服务器,这时会触发ioError事件。

另外,关于flash安全沙箱的知识,请参见官方权威文档《Flash Player 安全性》:http://help.adobe.com/zh_CN/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d23.html

发布了184 篇原创文章 · 获赞 13 · 访问量 71万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章