无法从red5服务器调用flash客户端上的方法

时间:2013-06-01 20:40:15

标签: red5 flash-builder

所以我坚持跟随问题。到目前为止,我可以从red5服务器上的客户端调用方法,但是从red5服务器调用客户端上的方法不起作用。我得到了以下代码

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";
  }

这是客户端闪存

现在在服务器端调用了sysout但是没有调用客户端的方法,出了什么问题?

    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 

}

1 个答案:

答案 0 :(得分:0)

您正在使用共享对象在客户端上调用该方法,但客户端SO未连接到服务器,下面的代码演示了如何执行此操作

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

当服务器连接成功时,需要调用此代码,因此应将其添加到onConnectionNetStatus函数中。