shockwave 预载技术
原著:lingoworkshop
翻译:alphachi
为了让程序拥有更强的适应性,我们还可以添加一.netdone()命令用来检测网络错误。“preloader”剧本的下一个版本能够检测任何网络错误。一旦发生错误,则使用一个参数返回错误的描述信息;如果没有错误,那么此参数的取值为一个空字符串。
"preloader" parent script (v.3)
property m.netid, mycallbackobj, mycompletionmsg
on new ( me ,.netaddress, callbackobj, completionmsg)
mycallbackobj = callbackobj
mycompletionmsg = completionmsg
m.netid = preloa.netthing .netaddress)
atimerobj = timeout ( me . string ). new ( 100 , # timer_checkprogress, me )
end
on timer_checkprogress ( me , atimer)
finished =.netdone (m.netid)
if finished then
errornotification = ""
errornum =.neterror (m.netid)
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(m.netid)函数获取网络操作的当前状态。这个函数会返回一个属性列表,其中包含像#state(可能是“connecting”或“in progress”)、字节总数和当前已传递字节数这样的信息。在“preloader”剧本的最终版本中,这些信息被用来确定url已被下载的部分。此版本附加了一个起始参数“statusmsg”——返回调用目标用于显示当前网络状况的程序名称。
"preloader" parent script (v.4)
property m.netid, mycallbackobj, mycompletionmsg, mystatusmessage
on new ( me ,.netaddress, callbackobj, completionmsg, statusmsg)
mycallbackobj = callbackobj
mycompletionmsg = completionmsg
mystatusmessage = statusmsg
m.netid = preloa.netthing .netaddress)
atimerobj = timeout ( me . string ). new ( 100 , # timer_checkprogress, me )
end
on timer_checkprogress ( me , atimer)
finished =.netdone (m.netid)
if finished then
errornotification = ""
errornum =.neterror (m.netid)
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 (m.netid)
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
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器