Skip to main content
November 10, 2010
Answered

problem when call a client side mothed from server side

  • November 10, 2010
  • 1 reply
  • 1405 views

Hi, everyone! I have a problem when I call a client side mothed from server side.

Below is the code. When the swf is connected to FMS(the connection is successful) I use this application.user_so.send("enterContestGroup"); to call the client side mothed. You can see that in the client side I have defined the "enterContestGroup" mothed. However the fact is it doesn't call that mothed. Can anybody tell me what the error is?

Thank you for any help!

This is the server  side code:

application.onAppStart = function()
{
    application.user_so = SharedObject.get("user_so", false);
    application.nextId = 0;
}
application.onConnect = function(newClient)
{
    application.acceptConnection(newClient);
    application.user_so.send("enterContestGroup");
}

This is the client side code:

var nc:NetConnection = new NetConnection();
var url:String = "rtmp://localhost:1935/testapp";
var user_so:SharedObject = new SharedObject();

nc.connect(url);

nc.onStatus = function(info)
     {
         if(info.code == "NetConnection.Connect.Success")
         {
             trace("success!");
    
         }
         else
         {
             trace(info.code)
         }
     }
user_so = SharedObject.getRemote("user_so", nc.uri, false);
user_so.connect(nc);

user_so.enterContestGroup = function(uid, uname, uimg, cid)
{
     trace("do something about enterRoom")
}

    This topic has been closed for replies.
    Correct answer

    Hi, Abhishek

    Thank you for your reply.

    My basic target is that when a user connect to FMS he can send a message(this message is not a string but it is an object, so I can't choose to use application.broadcastMsg) to all of the clients that have been connected to FMS including himself.


    Hi,

    Actually you can use broadcastMsg API to broadcast object to the clients.

    Here is an example of using it.

    Server side:

    var obj = new Object();
    application.onConnect = function(client) {

    obj.prop1 = "1";
    obj.prop2 = "2";
      application.acceptConnection(client);
    broadcast(obj);
    }

    var broadcast = function(obj) {
    application.broadcastMsg("m1",obj);
    }

    Client side (AS2)

    var nc = new NetConnection();
    nc.onStatus = function(info) {
    trace(info.code);
    }

    nc.m1 = function(obj) {
    trace(obj.prop1);
    trace(obj.prop2);
    }

    nc.connect("rtmp://localhost/test");

    Let me know if you face issues with this.

    Thanks,

    Abhishek

    1 reply

    November 10, 2010

    Hi,

    The problem here seems to be that you end up calling a function on shared object before shared object has actually been connected on client. You see, you are calling so.send in application.onConnect, but at that time sharedobject.getRemote and so.connect has not been executed on client. Thus, your call fails. You need to rethink your use case. Also it will be better practice to call so.getRemote and so.connect after recieving NetConnection.Connect.Success on client. Hope this clarified some of your doubts,

    Thanks,

    Abhishek

    November 11, 2010

    Hi, Abhishek

    Thank you for your answer. It seems explain it but in the server side there is no event that can decide if a client's sharedobject is connected to NetConnection successfully. So according to your reply I should not use so.send in the application.onConnect, right?

    November 11, 2010

    Hi,

    Actually I do not have a clear idea as to what you are trying to achieve from the shared objects, so I cannot give you the structure you can follow. If you use it inside application.onConnect the methods will be called on all the clients connected previously, but not necessarily to the particular client which caused the application.onConnect,i.e, let's say you have a client A connected already and it's shared object is created, when client B requests a connection it causes application.onConnect to trigger. So now, method will be called on A but not on B.

    You can give me a little detail as to what are you trying to achieve through shared object so that I can help you better. Also you can check the SharedObject.onSync handler and see if you can use this handler to refine your use case. This handler is trigerred on so.connect so you can use it too.

    Thanks,

    Abhishek