Search code examples
red5flash-builder

Can not call method on flash client from red5 server


So i got stuck on following problem. So far i can call methods from the client on the red5 server but calling methods on the client from red5 server is not working. I got the following code

public function onCreationComplete(event:FlexEvent) : void {


connection = new NetConnection();
connection.connect("rtmp://localhost/simpleChat");
connection.client = this;
so = SharedObject.getRemote("sharedMessage");
connection.addEventListener(NetStatusEvent.NET_STATUS, onConnectionNetStatus);
connection.call("addOne", ro, 5);
  }


  public function onConnectionNetStatus(event:NetStatusEvent) : void {

if(event.info.code == "NetConnection.Connect.Success") {
    Alert.show("Connection Successful","Information");
} else {
    Alert.show("Conection not successful", "Error");
}

        }

        public function onResult(responder:String): void{
Alert.show(responder);

  }

  public function onError(e:Object): void{
Alert.show("Got an error: " + e.description);
  }

  private function onClickSendBtn(event:MouseEvent):void
  {

connection.call("broadcastMessageToClients", null, inputTxt.text);
  }

  public function receiveBroadcastedMessages(msg:String):void
  {
            outputTxtArea.text += msg + "\n";
  }

this is the client side as a flash

and now on the server side the sysout is being called but the method on the client side is not called, what is wrong?

    public class Application extends ApplicationAdapter {

/*
 * The scope object. A statefull object shared between a group of clients connected to the same context path. 
 * Scopes are arranged in hierarchical way, so its possible for a scope to have a parent and children scopes. 
 * If a client connects to a scope then they are also connected to its parent scope. The scope object is used 
 * to access resources, shared object, streams, etc. That is, scope are general option for grouping things in 
 * application. The following are all names for scopes: application, room, place, lobby.
 */
private IScope appScope;


/** {@inheritDoc} */
@Override
public boolean connect(IConnection conn, IScope scope, Object[] params) {

    // init appScope
    appScope = scope;

    // create a sharedobject on server and call it "sharedMessage" under the current scope.
    createSharedObject(appScope, "sharedMessage", false);
    return true;
}

/** {@inheritDoc} */
@Override
public void disconnect(IConnection conn, IScope scope) {
    super.disconnect(conn, scope);
}

/* Simple method to illustrate how simple is to access the methods on the server side from the client side.
 * if called from the client it adds "1" to the passed argument.
 */
public double addOne(double a) {

    return a + 1;
}

/* Simple method to illustrate how simple is to access the methods on the client side from the server side.
 * Also this uses the SharedObject to send a unified message to all connected clients
 */

public void broadcastMessageToClients(List<String> params) {

    ISharedObject so = getSharedObject(appScope, "sharedMessage");

    System.out.println("broadcastMessageToClients...");
    // call receiveMessage method on all connected clients
    so.sendMessage("receiveBroadcastedMessages", params); // send the received parameter back to all connected clients by calling the "receiveBroadcastedMessages" method on the client side 

}

Solution

  • You are invoking the method on the client using a Shared Object but the client SO wasn't connected to the server, the code below demonstrate how to do it

    so = SharedObject.getRemote("sharedMessage");
    co.client = this; //indicate that this class will have the methods invoked by the server
    so.connect(connection); //connect the SO with the server
    

    This code need to be invoked when the connection is successful with the server, so it should be added to onConnectionNetStatus function.