Shockwave預載技術(二)


為了讓程序擁有更強的適應性,我們還可以添加一個netDone()命令用來檢測網絡錯誤。“Preloader”劇本的下一個版本能夠檢測任何網絡錯誤。一旦發生錯誤,則使用一個參數返回錯誤的描述信息;如果沒有錯誤,那么此參數的取值為一個空字符串。

"Preloader" Parent Script (v.3)

property myNetID, myCallBackObj, myCompletionMsg

on new (me, netAddress, CallBackObj, CompletionMsg)
  myCallBackObj = CallBackObj
  myCompletionMsg = CompletionMsg
  myNetID = preloadNetThing(netAddress)
  aTimerObj = timeout(me.string).new(100, #Timer_CheckProgress, me)
end

on Timer_CheckProgress (me, aTimer)
  finished = netDone(myNetID)
  if finished then
    errorNotification = ""
    errorNum = netError(myNetID)
    if integerP(errorNum) then
      if errorNum then
        errorNotification = me._GetError(errorNum)
      end if
    end if
    aTimer.forget()
    call(myCompletionMsg, myCallBackObj, errorNotification)
  else
    put "Still downloading"
  end if
end

on _GetError (me, errorCode)
  case errorCode of
    "OK", "", 0: return EMPTY
    4: ErrorMsg =( "Bad MOA class. The required Xtras are missing. ")
    5: ErrorMsg =( "The required Xtras are improperly installed or not installed at all.")
    6: ErrorMsg =( "Bad URL or the required Xtras are improperly installed. ")
    20: ErrorMsg =( "Internal error. The browser detected a network or internal error.")
    4146: ErrorMsg =( "Connection could not be established with the remote host.")
    4149: ErrorMsg =( "Data supplied by the server was in an unexpected format.")
    4150: ErrorMsg =( "Unexpected early closing of connection.")
    4154: ErrorMsg =( "Operation could not be completed due to timeout.")
    4155: ErrorMsg =( "Not enough memory available to complete the transaction.")
    4156: ErrorMsg =( "Protocol reply to request indicates an error in the reply.")
    4157: ErrorMsg =( "Transaction failed to be authenticated.")
    4159: ErrorMsg =( "Invalid URL.")
    4164: ErrorMsg =( "Could not create a socket.")
    4165: ErrorMsg =( "Requested object could not be found (URL may be incorrect).")
    4166: ErrorMsg =( "Generic proxy failure.")
    4167: ErrorMsg =( "Transfer was intentionally interrupted by client.")
    4242: ErrorMsg =( "Download stopped by netAbort(url).")
    4836: ErrorMsg =( "Download stopped for an unknown reason, possibly a network error, or the download was abandoned.")
    otherwise
      ErrorMsg =("Unknown error code")
  end case
  return ErrorMsg
end


  要想看到具體返回了什么參數,可以像下面這樣修改“PreloaderInterface”行為:

on beginSprite me
  clearcache()
  urlToLoad = "http://www.lingoworkshop.com/Tutorials/Preloader/Main.dcr"
  script("Preloader").new(urlToLoad, me, #mHandlePreloadCompletion)
end

on mHandlePreloadCompletion (me, errorMsg)
  if errorMsg <> EMPTY then alert "Network Error!" & return & errorMsg
  else alert "All Done"
end


  現在,”Preloader”劇本已經有能力預載一個URL並在預載完成時對另一個對象進行返回調用。最后一步則是讓“Preloader”劇本可以報告其運行狀態,以便我們制作一個進度條來給用戶提供一些反饋信息。為了達到這一目的,我們可以使用getStreamStatus(myNetID)函數獲取網絡操作的當前狀態。這個函數會返回一個屬性列表,其中包含像#state(可能是“connecting”或“in progress”)、字節總數和當前已傳遞字節數這樣的信息。在“Preloader”劇本的最終版本中,這些信息被用來確定URL已被下載的部分。此版本附加了一個起始參數“StatusMsg”——返回調用目標用於顯示當前網絡狀況的程序名稱。

"Preloader" Parent Script (v.4)

property myNetID, myCallBackObj, myCompletionMsg, myStatusMessage

on new (me, netAddress, CallBackObj, CompletionMsg, StatusMsg)
  myCallBackObj = CallBackObj
  myCompletionMsg = CompletionMsg
  myStatusMessage = Statusmsg
  myNetID = preloadNetThing(netAddress)
  aTimerObj = timeout(me.string).new(100, #Timer_CheckProgress, me)
end

on Timer_CheckProgress (me, aTimer)
  finished = netDone(myNetID)
  if finished then
    errorNotification = ""
    errorNum = netError(myNetID)
    if integerP(errorNum) then
      if errorNum then
        errorNotification = me._GetError(errorNum)
      end if
    end if
    aTimer.forget()
    call(myCompletionMsg, myCallBackObj, errorNotification)
  else
    -- 仍在預載;檢測是否需要報告當前狀態
    if myStatusMessage.ilk = #Symbol then
      -- 已經得到一個返回調用消息;獲取狀態列表並將其發送給返回調用目標
      theStatus = getStreamStatus(myNetID)
      currentState = theStatus.state
      -- 在發送狀態列表前計算出已下載部分(變量“fractionDone”)
      case (currentState) of
        "InProgress":
          if theStatus.bytesSoFar > 0 then
            if theStatus.bytesTotal > 0 then fractionDone = MIN(1.0, float(theStatus.bytesSoFar) / theStatus.bytesTotal )
            else fractionDone = 50
          else
            fractionDone = 0
          end if
        "Complete":
          fractionDone = 100
        otherwise
          fractionDone = 0
      end case
      -- 將fractionDone作為一個屬性添加到狀態列表中
      theStatus.addProp( #fractiondone, fractionDone )
      -- 通知當前狀態的返回調用對象和已傳遞的百分比
      call(myStatusMessage, [myCallbackObj], theStatus)
    end if
    
  end if
end

on _GetError (me, errorCode)
  case errorCode of
    "OK", "", 0: return EMPTY
    4: ErrorMsg =( "Bad MOA class. The required Xtras are missing. ")
    5: ErrorMsg =( "The required Xtras are improperly installed or not installed at all.")
    6: ErrorMsg =( "Bad URL or the required Xtras are improperly installed. ")
    20: ErrorMsg =( "Internal error. The browser detected a network or internal error.")
    4146: ErrorMsg =( "Connection could not be established with the remote host.")
    4149: ErrorMsg =( "Data supplied by the server was in an unexpected format.")
    4150: ErrorMsg =( "Unexpected early closing of connection.")
    4154: ErrorMsg =( "Operation could not be completed due to timeout.")
    4155: ErrorMsg =( "Not enough memory available to complete the transaction.")
    4156: ErrorMsg =( "Protocol reply to request indicates an error in the reply.")
    4157: ErrorMsg =( "Transaction failed to be authenticated.")
    4159: ErrorMsg =( "Invalid URL.")
    4164: ErrorMsg =( "Could not create a socket.")
    4165: ErrorMsg =( "Requested object could not be found (URL may be incorrect).")
    4166: ErrorMsg =( "Generic proxy failure.")
    4167: ErrorMsg =( "Transfer was intentionally interrupted by client.")
    4242: ErrorMsg =( "Download stopped by netAbort(url).")
    4836: ErrorMsg =( "Download stopped for an unknown reason, possibly a network error, or the download was
abandoned.")
    otherwise
      ErrorMsg =("Unknown error code")
  end case
  return ErrorMsg
end

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com