Flash Media Server 教程(3)
2012-05-10 10:23
393 查看
客户端呼叫服务器
fla:
服务器端代码是放在main.asc里的,你可以到你的application下的你的目录下建一个main.asc,写代码
main.a
服务器端呼叫客户端
fla:
main.asc:
服务器端很少要求客户端返回值,如果一定要返回,一本书上是这么写的,没细看
Server端與Client端method觸發與data廣播概念 by ozzysun
在FCS的應用上常會需要讓Client與Server間的資料傳遞與method呼叫,以下幾點概念應該可以
避免呼叫method沒反應或資料沒傳到你要的地方等狀況發生。
一.Server端method如何被呼叫
Server端上要能讓Client所呼叫觸發的function,需定義在Server端的Client物件上你可以用
prototype方式去extend原本Client類別的method,讓Client類別所產生的instance都擁有該
method。
例:
Client.prototype.newfun=function(){}
你也可以把這function定義在單一Client instance下,
例:
application.onConnect(newClient)=function(){
.....
newClient.newfun=function(){}
}
二.Client端的method如何被呼叫
在Client端上要讓Server端可以呼叫的function,一定要定義在Netconnection上。
例:
nc=new NetConnection();
.....
nc.myfun=function(){}
三.send或call這兩個method在使用上有何差異? 如何使用
call:這method在Client端上可使用的是netconnection物件,在Server端上可使用的是
netconnection與Client物件
1.在Client端利用netconnection.call來觸發執行Server端上Client 物件的method。
2.在Server端上利用Client物件.call來觸發Client端上netconnection上的method。
3.在Server端上使用netconnection.call時,這時這server的角色就像一個client端一樣
,是在觸發另一個Server端上Client 物件的method。
send:在Client端上可使用這method的包含SharedObject與netStream
send這個method很有趣,他讓你由client端去啟動所有同在client端的function,但因為
在觸發function時可以帶參數過去,這個特性是可以讓你利用來做小量資料的廣播的,要
廣播給所有人接收到的資料,並不一定就要放在SharedObject內,利用其onSync來做同步
,有時用send也是一個很簡單的做法,如何定義
1.在netStream或SharedObject上定義好method "myfun"
2.利用netStream.send("myfun")或sharedobject.send("myfun",myvar)可讓所有client
上的"myfun"都會被觸發
四.幾種可能的互動型態範例
1.Client端對Server端傳送data或呼叫執行Server端function
應用範例:一個簡易聊天室,聊天內容只存在Server端的變數內,不使用SharedObject存放呼
叫Server端的message這method來處理client傳上去的msg這對話內容
Client端:
nc.call("message", null, msg);
Server端:
application.onAppstart=function(){
application.chat_content="";
}
application.onConnect=function(newClient){
.......
newClient.message=function(msg){
application.chat_content+=msg;
}
}
2.Client端對所有Client廣播data並執行指定Client端function
應用範例:以之前在站上回覆過的問題為例,一個clinet要輸入一個網址url_txt,要讓所有的
client都會開啟這網址的網頁
Client端:
先定義一個附掛在so上的method
lobby_so.openPage=function(receive_url){
getURL(receive_url);
}
利用send就可讓所有client接收到這網址並開啟
lobby_so.send("openPage",url_txt);
3.Server傳送data給特定Client
應用範例:當有使用者連線上server,當使用者資料驗證正確時,接受其連線,一方面要client
去執行指定的function跳到某頁或讓某mc出現...,同時又要把server端的資料帶過去
說明:server只回應正在與server做互動的那個client,如以上範例,server只會去觸發請求
連線的該client去執行指定的function,其他client不會有反應
Server端:
application.onConnect=function(newClient,pwd){
if(pwd=="ok"){
application.acceptConnection(newClient);
newClient.call("get_message",null,message);
}else{
application.rejectConnection(newClient,errObj);
}
}
Client端:
........
nc.get_message=function(message){};
4.Server廣播data給所有Client
應用範例:當有人斷線時,由server端廣播所有client,讓所有client都能同步更新client名單
說明:以上範例來說,當clinet無預警的斷線,只有Server上的application.onDisconnect 這handler
會被觸發,也就是說你需要在這handler內寫一些程式去廣播通知給所有的client。
如何廣播?有以下兩種做法
1.把資料放在remote SharedObject物件內,只要SO物件內容更動,即自動觸發Client端的so.onSync
將線上人員名單寫在remote SharedObject物件內,當有人斷線,只要把so內該筆資料剔除掉,因為
so內容改變,因此所有Client端的so.onSync 這handler將被觸發,即可達到你要更新資料的目的。
Server端
application.onDisconnect=function(newClient){
userlist_so.setProperty(newClient.name,"");
}
註:相對的當server端無預警的斷線,client端可由nc.onStatus這handler內由判斷info.code來取
得資訊
2.當資料不是存在so內時,只是存在server端的一個變數上,可以善加利用application.clients來對
所有client廣播。以下會觸發所有client端上的client_fun,並把server上的sendvar變數帶過去
server端:
application.onDisconnect=function(newClient){
for(var i=0;i<application.clients.length;i++) {
application.clients[i].call("client_fun",null,sendvar);
}
}
Client端:
nc.client_fun=function(myvar){ }
增加一個廣播的方法
Server 端傳送給有getRemote 同一個ShareObject 的Client 端
Server 端:
application.abc_so = SharedObject.get("abc_so", false);
application.abc_so.send("msgFromSrvr", msg);
Client 端:
abc_so = SharedObject.getRemote("abc_so", abc_nc.uri, false);
abc_so.msgFromSrvr = function(msg) {
showMsg(msg);
};
学了就要用。练练吧,做个在线列表,这次没有用ShareObject而是把用户名都装到一个application.onlineList数组里
通过遍历application.clients来广播给各个client,上线下线就是对onlinelist数组的处理。看看吧
Client:
server端
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.a
sc: //要把函数定义到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)); };
Server端與Client端method觸發與data廣播概念 by ozzysun
在FCS的應用上常會需要讓Client與Server間的資料傳遞與method呼叫,以下幾點概念應該可以
避免呼叫method沒反應或資料沒傳到你要的地方等狀況發生。
一.Server端method如何被呼叫
Server端上要能讓Client所呼叫觸發的function,需定義在Server端的Client物件上你可以用
prototype方式去extend原本Client類別的method,讓Client類別所產生的instance都擁有該
method。
例:
Client.prototype.newfun=function(){}
你也可以把這function定義在單一Client instance下,
例:
application.onConnect(newClient)=function(){
.....
newClient.newfun=function(){}
}
二.Client端的method如何被呼叫
在Client端上要讓Server端可以呼叫的function,一定要定義在Netconnection上。
例:
nc=new NetConnection();
.....
nc.myfun=function(){}
三.send或call這兩個method在使用上有何差異? 如何使用
call:這method在Client端上可使用的是netconnection物件,在Server端上可使用的是
netconnection與Client物件
1.在Client端利用netconnection.call來觸發執行Server端上Client 物件的method。
2.在Server端上利用Client物件.call來觸發Client端上netconnection上的method。
3.在Server端上使用netconnection.call時,這時這server的角色就像一個client端一樣
,是在觸發另一個Server端上Client 物件的method。
send:在Client端上可使用這method的包含SharedObject與netStream
send這個method很有趣,他讓你由client端去啟動所有同在client端的function,但因為
在觸發function時可以帶參數過去,這個特性是可以讓你利用來做小量資料的廣播的,要
廣播給所有人接收到的資料,並不一定就要放在SharedObject內,利用其onSync來做同步
,有時用send也是一個很簡單的做法,如何定義
1.在netStream或SharedObject上定義好method "myfun"
2.利用netStream.send("myfun")或sharedobject.send("myfun",myvar)可讓所有client
上的"myfun"都會被觸發
四.幾種可能的互動型態範例
1.Client端對Server端傳送data或呼叫執行Server端function
應用範例:一個簡易聊天室,聊天內容只存在Server端的變數內,不使用SharedObject存放呼
叫Server端的message這method來處理client傳上去的msg這對話內容
Client端:
nc.call("message", null, msg);
Server端:
application.onAppstart=function(){
application.chat_content="";
}
application.onConnect=function(newClient){
.......
newClient.message=function(msg){
application.chat_content+=msg;
}
}
2.Client端對所有Client廣播data並執行指定Client端function
應用範例:以之前在站上回覆過的問題為例,一個clinet要輸入一個網址url_txt,要讓所有的
client都會開啟這網址的網頁
Client端:
先定義一個附掛在so上的method
lobby_so.openPage=function(receive_url){
getURL(receive_url);
}
利用send就可讓所有client接收到這網址並開啟
lobby_so.send("openPage",url_txt);
3.Server傳送data給特定Client
應用範例:當有使用者連線上server,當使用者資料驗證正確時,接受其連線,一方面要client
去執行指定的function跳到某頁或讓某mc出現...,同時又要把server端的資料帶過去
說明:server只回應正在與server做互動的那個client,如以上範例,server只會去觸發請求
連線的該client去執行指定的function,其他client不會有反應
Server端:
application.onConnect=function(newClient,pwd){
if(pwd=="ok"){
application.acceptConnection(newClient);
newClient.call("get_message",null,message);
}else{
application.rejectConnection(newClient,errObj);
}
}
Client端:
........
nc.get_message=function(message){};
4.Server廣播data給所有Client
應用範例:當有人斷線時,由server端廣播所有client,讓所有client都能同步更新client名單
說明:以上範例來說,當clinet無預警的斷線,只有Server上的application.onDisconnect 這handler
會被觸發,也就是說你需要在這handler內寫一些程式去廣播通知給所有的client。
如何廣播?有以下兩種做法
1.把資料放在remote SharedObject物件內,只要SO物件內容更動,即自動觸發Client端的so.onSync
將線上人員名單寫在remote SharedObject物件內,當有人斷線,只要把so內該筆資料剔除掉,因為
so內容改變,因此所有Client端的so.onSync 這handler將被觸發,即可達到你要更新資料的目的。
Server端
application.onDisconnect=function(newClient){
userlist_so.setProperty(newClient.name,"");
}
註:相對的當server端無預警的斷線,client端可由nc.onStatus這handler內由判斷info.code來取
得資訊
2.當資料不是存在so內時,只是存在server端的一個變數上,可以善加利用application.clients來對
所有client廣播。以下會觸發所有client端上的client_fun,並把server上的sendvar變數帶過去
server端:
application.onDisconnect=function(newClient){
for(var i=0;i<application.clients.length;i++) {
application.clients[i].call("client_fun",null,sendvar);
}
}
Client端:
nc.client_fun=function(myvar){ }
增加一個廣播的方法
Server 端傳送給有getRemote 同一個ShareObject 的Client 端
Server 端:
application.abc_so = SharedObject.get("abc_so", false);
application.abc_so.send("msgFromSrvr", msg);
Client 端:
abc_so = SharedObject.getRemote("abc_so", abc_nc.uri, false);
abc_so.msgFromSrvr = function(msg) {
showMsg(msg);
};
学了就要用。练练吧,做个在线列表,这次没有用ShareObject而是把用户名都装到一个application.onlineList数组里
通过遍历application.clients来广播给各个client,上线下线就是对onlinelist数组的处理。看看吧
Client:
btn.onRelease = function() { if (_root.id.text != "") { _root.init(); } }; function init() { IDNUM = _root.id.text; //这个为登陆名 //IDPASS = "456"; mync = new NetConnection(); mync.onStatus = function(info) { trace(info.code); }; mync.connect("rtmp://localhost/phone", IDNUM, IDPASS); mync.onlineList = function(list) { //填充List组件 mylist.labels = list; }; }
server端
application.onAppStart=function(){ trace("------onAppStart----") this.onlineList=[] } application.onConnect=function(newClient,IDNUM,IDPASS){ trace("------user onconnected------") newClient.IDNUM=IDNUM //newClient.IDPASS=IDPASS //处理在线数组(push) this.onlineList.push(IDNUM) this.acceptConnection(newClient); //广播给client for(var i=0;i<this.clients.length;i++){ this.clients[i].call("onlineList",null,this.onlineList) } } application.onDisconnect = function (newClient){ trace("------user disconnected------"); //处理在线数组(splice) for(var i=0;i<=this.clients.length;i++){ if(this.onlineList[i]==newClient.IDNUM){ this.onlineList.splice(i,1) } } //广播 for(var i=0;i<this.clients.length;i++){ this.clients[i].call("onlineList",null,this.onlineList) } };
相关文章推荐
- Flash Media Server 入门教程
- Flash Media Server安装与使用入门教程(转)
- Flash Media Server 教程(1)
- Flash Media Server 入门教程
- Flash Media Server 教程(2)
- Flash Media Server 入门教程
- Flash Media Server 入门教程
- Flash Media Server 一些教程
- Flash Media Server 入门教程
- Flash Media Server 入门教程【很好的教程】
- Flash Media Server安装与使用入门教程
- [FMS]一步一步学Flash Media Server(四)
- Flash Media Server 4.5带来Flash末日?
- 二、采用Flash Media Server3.5对视频进行流式处理
- FMS(Flash Media Server)共享内存问题解决
- Adobe/Flash Media Server 5.0 linux 64位系统下的安装
- Adobe/Flash Media Server 5.0 linux 64位系统下的安装
- 如果你的第一个Flash Media Server 的 HelloWorld程序失败了……
- FLEX利用FLASH MEDIA SERVER进行视频的在线录制和回放
- Flash Media Server 起步(13)再说说SO....onSync(list)