Hi All,
This is an ActionScript problem for a Flash Remoting app... but it's not a
Remoting issue (or so I think) so I'm posting it here.
This is a Chat Application done all in Flash that uses several different .swf
files (1 for each page). I have no trouble passing the user id from .swf to
..swf, but the main app page (chat.swf) is where I have the problem.
This is the problem. I have several _global.variables that are set inside a
function that returns query results from a CF component. They are available
some of the time (inside certain other functions), but not all of the time
(inside certain other functions or outside of any functions)!? For now I don't
need them in other functions, but found that out through testing. I only need
them in one line that [b]normally[/b] appears outside of any function:
[b]client_nc.connect("rtmp://
www.severname.com/chat/"+_global.rname,
_global.uname);[/b]
this line sends the room name, user name to the Flash Communication Server
(FCS). If I hard code it:
client_nc.connect("rtmp://
www.severname.com/chat/+[b]"Room Name", "User
Name"[/b]);
it works fine, so it's not on the FCS server side. Like I said before, the
variables are sometimes available, only not where and when I need them. This is
puzzling since I thought Global Variable were ALWAYS available... yes? I guess
not.
Here is the entire code (I put relevant comments and code in [b] bold[/b]):
________________________________________________________________________________
_______
stop();
[b] // added for testing inside Flash authoring. normally this variable is
passed via url when a new chat room is created
nrid = 77;[/b]
_root.logout_btn.onRelease = function() {
getURL("index.html", "_self");
}
#include "NetServices.as"
if (isGatewayOpen == null) {
// do this code only once
isGatewayOpen = true;
NetServices.setDefaultGatewayUrl("
http://127.0.0.1:8500/flashservices/gateway");
gatewayConnnection = NetServices.createGatewayConnection();
login = gatewayConnnection.getService("poetry.login", this);
chat_rm = gatewayConnnection.getService("poetry.chat", this);
if (nrid !== undefined) {
uid = nrid
chat_rm.getNewRoomName(nrid)
}
if (uid !== undefined){
// if user is logged in (see above), automatically sends variable uid to
login.cfc.
// searches by userid (uid) to get variables from users db.
login.GetName(uid);
}
}
// sets global chat room variables for use in this movie and for passing to
other movies
[b]// first set of global variables retrieved in this function
function getNewRoomName_result(result){ [/b]
_global.rmid = result.items[0].chatid;
_global.rname = result.items[0].chat_title;
_global.rdesc = result.items[0].chat_desc;
_global.ruser = result.items[0].userid;
_global.rtime = result.items[0].rm_date;
[b]// here the variables work, whether or not I use them with _global. prefix
_root.content_mc.room_name.text = rname;[/b]
}
// sets global user variables for use in this movie and for passing to other
movies
[b]// second set of global variables retrieved in this function
function GetName_result(result){[/b]
_global.uid = result.items[0].userid;
_global.fname = result.items[0].first_name;
_global.lname = result.items[0].last_name;
_global.fullname = result.items[0].first_name+" "+result.items[0].last_name;
_global.uname = result.items[0].user_name;
}
[b]// function added only for testing code inside a function... no go
function doChat (){[/b]
#include "NetDebug.as"
// Set maximum scroll
History.maxscroll = 1000;
// Don?t allow the user to send until after connection
Send_btn.setEnabled(false);
// Open connection to the server
client_nc = new NetConnection();
// If connection is closed, clear the History and the list
client_nc.onStatus = function(info) {
trace("Level: "+info.level+newline+"Code: "+info.code);
if (info.description == "NetConnection.Connect.Closed") {
History.text = "";
People.removeAll();
}
};
// Connect to the chat application.
[b]// This is where I need them, but
the variables don't work... no matter what I do?!
//(i.e. inside a function,
outside, _global. prefix or no)
client_nc.connect("rtmp://research.parsons.edu/julia/"+_global.rname,_global.u
name);
//nothing here either... trace
returns 'undefined'
trace (_global.uname);[/b]
// Update button label
Connect_btn.setLabel("Exit");
// Enable send button
Send_btn.setEnabled(true);
// Create a remote shared object to keep track
// of the users. The value client_nc.uri is the URI of the
// NetConnection the shared object will use to connect to the
// server. I.e., the one just created.
users_so = SharedObject.getRemote("users_so", client_nc.uri, false);
// Attach the shared object to 'client_nc'
users_so.connect(client_nc);
// When the list of users_so is updated, refresh the
// 'People' list box.
users_so.onSync = function(userList) {
People.removeAll();
for (var i in users_so.data) {
if (users_so.data[i] != null) {
People.addItem(users_so.data[i]);
}
}
// Sort alphabetically, because order returned
// is not guaranteed to be consistent.
People.sortItemsBy("label", "ASC");
};
// Update the shared object with the message.
users_so.msgFromSrvr = function(msg) {
History.text += msg;
History.scroll = History.maxscroll;
historyScroll.setScrollTarget(history);
historyScroll.setScrollPosition(History.maxscroll);
};
}
[b]// added to execute the above function for testing
doChat.apply();[/b]
// Send the message text by calling the server function 'message'
function doSend() {
// If there's message text, pass it to the server function 'msgFromClient'
if (length(Message.text)>0) {
client_nc.call("msgFromClient", null, Message.text);
// and insert into the db w/ room & user id's
chat_rm.insertLine(rmid, uid, chat_t)
[b]// here the variables work, whether or not I use them with _global. prefix
_root.content_mc.welcome.text = (fname);
trace (lname)[/b]
}
// Clear the message text
Message.text = "";
}
function doSave() {
// insert into the db flag to archive the poem
chat_rm.putSaveChat(rmid)
// Update the History on the server with the message
[b]// here the variables work... but only if I use them with _global. prefix?!
History.text = _global.rname+" SAVED";
trace (_global.rmid)[/b]
};
_root.login_mc.logout_btn.onRelease = function() {
client_nc.close();
People.removeAll();
Message.text = "";