More on Nonpersistent Client-Side Remote Shared Objects - Minimalist Project for Shared Function
(Page 2 of 4 )
This next little minimalist project is very cool. It allows you to open a web page on someone else’s browser. You can open up a web page from any domain, not just your local one. Here are the classes and objects you’ll use:
Classes
NetConnection
SharedObject
Objects
TextInput Component (1)
Button Component (1)
In setting up a link to another URL, ActionScript employs thegetURL(url)function. Because the function’s argument is the string with the URL, the argument will be the shared object. Figure 4-4 shows a chunk of code from the project that traces the changing of the shared object to the firing of the function. The three key elements are numbered from 1 to 3.
In step 1, a function is defined that includes a shared object data attribute in the definition. In step 2, an unnamed function (webPageobject) assigns the shared object a new value when the user presses a button and fills in an InputText component. In step 3, the change in the shared object triggers theSharedObject.onSyncfunction that contains thegetURL(url)function. Thus, all connections open up the same web page. Use Figure 4-5 as a guide and use the following steps to create the full application:

Figure 4-4. Path of shared object events

Figure 4-5. Objects and instant names
Open up a new Flash document and set up the following layers, from top to bottom: Actions, Text Input Component, Button, and Label.
- In the Text Input Component layer, add a TextInput component and provide it with the instance nameurl_ti.
- Add a button to the Button layer and give it the instance nameurl_button.
- Add the label Enter URL Name (without http://) using static text on the Label layer. Add a logo in the upper left corner.
Click the first frame of the Actions layer and add the following script:
1 var rtmpNow:String;
2 rtmpNow="rtmp:/zbasicSO";
3 //rtmpNow = "rtmp://192.168.0.11/zbasicSO";
4 var nc:NetConnection = new NetConnection();
5 nc.onStatus = function(info) {
6 trace(info.code);
7 };
8 nc.connect(rtmpNow);
9 //Shared Object
10 var webpg_so:SharedObject = SharedObject.getRemote("showWeb", nc.uri, false);
11 webpg_so.onSync = function() {
12 showURL(webpg_so.data.urlName); 13 };
14 webpg_so.connect(nc);
15 //Open URL
16 var defaultURL:String = "www.sandlight.com";
17 url_ti.text = defaultURL;
18 url_button.label = "Show Web Page"; 19 showURL = function (urlNow) {
20 if (urlNow) {
21 getURL("http://"+webpg_so.data.urlName,"_blank"); 22 }
23 };
24 var webPage:Object = new Object(); 25 webPage.click = function() {
26 webpg_so.data.urlName = url_ti.text;
27 url_ti.text = "";
28 };
29 url_button.addEventListener("click",webPage);
When you run the program on a single machine with two instances, you will get two of everything, but run with two or more computers, you will see a web page appear for all your connections.
Next: A More Than Minimal Text Chat >>
More Flash Articles
More By O'Reilly Media