關於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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章