2013-06-01 106 views
1

因此,我陷入了下面的問題。到目前爲止,我可以從red5服務器上的客戶端調用方法,但從red5服務器調用客戶端上的方法不起作用。我得到了下面的代碼無法從red5服務器調用flash客戶端的方法

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

這是客戶端作爲閃存

,現在在服務器端的系統輸出被調用,但在客戶端的方法不叫,有什麼不好?

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 

} 

回答

0

您使用的是共享對象,但客戶端,因此並沒有連接到服務器調用客戶端的方法,下面的代碼演示瞭如何做到這一點

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函數中。

相關問題