PDA

View Full Version : remote shared objects problem


matt poole
10-21-2003, 11:52 AM
Im trying to store multiple levels of data in a shared object by using a reference to an array in the setProperty() method in server-side AS.

I know the data is going in cos I can trace it using the netConnection debugger. The probelm is reading the data out of the so on the client.

Can anyone see whats going wrong??

heres the SS AS
application.onAppStart = function()
{
//Get the server shared object dataso
application.dataso = SharedObject.get("dataso", false);

LocalClientData = new Array();
trace("Array Created");
application.dataso.setProperty("RemoteClientData", LocalClientData);
application.nextId = 0;
}

application.onConnect = function(newClient, lastname, firstname)
{

trace("Next ID: " + application.nextId);
newClient.id = application.nextId;
trace("New Client ID: " + newClient.id);
application.acceptConnection(newClient);
trace("Accepted Client connection");
trace("Number of Clients: " + application.clients.length);
LocalClientData[newClient.id] = new Array();
LocalClientData[newClient.id][0] = firstname;
LocalClientData[newClient.id][1] = lastname;
trace("Client Property Array added to ClientData Array");

application.dataso.setProperty("RemoteClientData", LocalClientData);
application.nextId++;

//Proves that each client exists in the shared obj
newarr = application.dataso.getProperty("RemoteClientData");
for ( var i in newarr) {
for ( var j in newarr[i]) {
trace(newarr[i][j]);
}
}


}

application.onDisconnect = function(client)
{
trace("Client ID: " + LocalClientData[client.id][0] + " " + LocalClientData[client.id][1] + " has disconnected");
}



heres the client side:

#include "NetDebug.as"

// 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);


}
function doConnect() {
if (Connect_btn.getLabel() == "Login") {

// is the application instance.
_root.client_nc.connect("rtmp:/class_shared_objects/myInstance", _root.surname.text, _root.firstname.text);

// attached connection light to net obj
//light_mc.connect(client_nc);

// Update button label
Connect_btn.setLabel("Logout");

// 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.
dataso = SharedObject.getRemote("dataso", _root.client_nc.uri, false);
// Attach the shared object to 'client_nc'
dataso.connect(_root.client_nc);


//Read from shared obj
trace(dataso.data[0]); // <= heres the problem bit





} else if (Connect_btn.getLabel() == "Logout") {

// Close connection
_root.client_nc.close();

// Rest button label
Connect_btn.setLabel("Login");
}
}


cheers for your help:)

praveenghatta
11-30-2007, 11:01 AM
Hi Matt,
I found your problem similar to mine.
Here I have a small bit of information which could be useful to you in case you want to output, the array stored in a shared object present on the server.

You could probably do two things for it!..

1. Create a local object on the cleint side and sync it with your shared object on the server.

2. A better way...
Connect to the server using a function
msgfrmcleint()
and then send it back to the client a message from the server called:
msgfrom server()

Here is the code for the second one...:

Server Side:....
application.onAppStart = function()
{
trace("Begin sharing text");

// Get the server shared object 'users_so'
application.users_so = SharedObject.get("ChatUsers");

// Initialize the history of the text share
application.history = "";

// Initialize the unique user ID
application.nextId = 0;
}

application.onConnect = function(newClient, userName)
{
// Make this new client's name the user's name
newClient.name = userName;

// Create a unique ID for this user while incrementing the
// application.nextID.
newClient.id = "u" + application.nextId++;

// Update the 'users_so' shared object with the user's name
application.users_so.setProperty(newClient.name, userName);

// Accept the client's connection
application.acceptConnection(newClient);



// Call the client function 'setHistory,' and pass
// the initial history
newClient.call("setHistory", null, application.history);

// The client will call this function to get the server
// to accept the message, add the user's name to it, and
// send it back out to all connected clients.
newClient.msgFromClient = function(msg) {
msg = userName + ": " + msg + "\n";
application.history += msg;
application.users_so.send("msgFromSrvr", msg);
}
}

application.onDisconnect = function(client)
{
trace("disconnect: " + client.name);
application.users_so.setProperty(client.name, null);
application.users_so.flush();
}

Cleint Side code is:

private function sendMessage():void{
// call our remote function and send the message to all connected clients
nc.call("msgFromClient", null, sendMessageInput.text);
sendMessageInput.text = "";
}

Now recieve the mesage here in the following:
public function msgFromSrvr(msg:String):void{
writeMessage(msg); // Here you can obtain the data and use it elsewhere...

}
I hope this helped u....
dont mind if it didnt..!!!