Articles Archive
Articles Search
Director Wiki
 

Queueing Net Operations

March 12, 2001
by Will Turnage

Dear Multimedia Handyman,

I'm trying to use getNetText in my movie to download some information off the Web. I know that I need to check for netDone, but I'm not sure how to do this. Can you help?

Travis

Dear Travis,

Whether you're making a shockwave movie or a projector, Director's ability to download information from the Internet is a feature that can add a lot of power to your project. You can easily update content without having to rebuild your projector; which can save your clients time and money. For the beginner, though, Internet operations are not always that easy to deal with.

So for this week's column, we're going to try something a little different. Rather than walk you through how to build your own solution, I'm just going to let you download the code that does the work for you. You can read here how to implement the code in your project, and if people are interested enough in learning how it works, then that will be covered in another column.

Essentially, this code is a Internet operations queue. It will execute your Internet downloads for you, as well as monitor all currently running operations. It will check for errors, and when the operation is complete, the code will automatically call any handler in any object that you specify. So if you use this code in your project, you shouldn't have to write any extra code to monitor net connections.

To use this object, begin by downloading the sample movie. When you open the movie, the first two cast members in your movie will be parent scripts. One is named "DOUG netQueue object" and the other is named "DOUG netOperations object". Copy these two scripts to your own movie.

The first code you should create in your own movie will create and destroy the netQueue object when the movie begins and when the movie ends. That code looks like this:

global gNetQueue

on prepareMovie

  gNetQueue = new (script "netQueue object")

end

on stopMovie

  gNetQueue.destruct ()
  gNetQueue = 0

end

The first line of the prepareMovie handler creates the netQueue object and stores it in a global variable called gNetQueue. The first line of the stopMovie handler tells the netQueue object to disconnect from any currently running operations. Finally, you set the object equal to zero, thus eliminating it from memory.

Once the object is created, then you can use it instead of these four Network Lingo commands: getNetText, postNetText, preloadNetThing, and downloadNetThing. There will only be two differences between your code and the normal Network Lingo you call.

First you will always have to refer to the netQueue object when you call any Network Lingo command. Second, you will add two extra parameters to the end of each call. The first parameter is the handler you want called when the download is finished; the second optional parameter is the object that contains the handler.

For example, using normal Network Lingo, you would add this code to your behavior:

on mouseUp me

  getNetText ("http://www.director-online.com")

end

With the netQueue object, the code in your behavior would look like this:

global gNetQueue

on mouseUp me

  gNetQueue.getNetText ("http://www.director-online.com", #doneDownloading, me)

end

on doneDownloading me, theResult

  put theResult

end

The biggest difference here is that with the normal Network Lingo commands, you would have to write additional code that checks the netDone function to see if your operation is finished, then check the netTextResult function to retrieve the text you downloaded. With the netQueue object, when the text is done downloading it will automatically call the handler you specified (in this case doneDownloading) in any object you specify (in this case me, meaning the instance of the behavior that contains the code).

You don't have to specify an object though, if you want, you can just specify a single handler. For instance, if your code said:

on mouseUp me

  gNetQueue.getNetText ("http://www.director-online.com", #doneDownloading)

end

then the netQueue object would call the doneDownloading handler in your movie scripts instead of one found within an object or a behavior.

A sample Director 7 movie is available for download in Mac or Windows format.

Finally, if you'd like to see more articles like this in the Handyman column, then send an email to will / at / director-online.com, and I'll see what we can do. Happy coding!

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.