|
这是目前为止最难的了吧~~
源码: code.rar
整理后就几行代码不过多解释
客户端呼叫服务器
fla:
//客户端呼叫server端msgfromclient函数,并将返回值trace出来 mync = new NetConnection(); mync.connect("rtmp://localhost/connect"); //返回值 var resObj = new Object(); resObj.onResult = function(val):Void { trace("val"+val); }; mync.call("msgfromclient", resObj, "第一个call");
服务器端代码是放在main.asc里的,你可以到你的application下的你的目录下建一个main.asc,写代码
main.asc:
//要把函数定义到Client上!! application.onConnect = function(client) { /* 在这里定义也可以,在Client.prototype里定义也可以 client.msgfromclient=function(what){ trace(what+"进来了")
var aa="呼叫成功并返回结果" return aa } */ application.acceptConnection(client); };
Client.prototype.msgfromclient=function(what){ trace(what+"进来了")
var aa="呼叫成功并返回结果" return aa }
服务器端呼叫客户端
fla:
//server呼叫client端 //要把函数定义到nc上!! // mync = new NetConnection(); mync.onStatus = function(info) { if (info.code == "NetConnection.Connect.Success") { trace("连接成功"); } }; mync.connect("rtmp://localhost/connect"); mync.msgfromserver = function(msg) { trace(msg); };
main.asc:
application.onConnect = function(client) { application.acceptConnection(client); client.call("msgfromserver",null,"服务器叫你啊") };
服务器端很少要求客户端返回值,如果一定要返回,一本书上是这么写的,没细看
// Define a class that just stores the client ID. AreYouOkResultHandler = function (clientID) { this.clientID = clientID; }; // Handle the result of calling areYouOk( ). AreYouOkResultHandler.onResult = function (val) { trace("Client " + this.clientID + " returned " + val); };
application.pingClient = function (clientObj, clientID) { // Invoke a message on the client. clientObj.call("areYouOk", new AreYouOkResultHandler(clientID)); }; |