Server端与Client端method触发与data广播概念

 Server端与Client端method触发与data广播概念

by ozzysun  出自:http://www.streetvoice.com.tw/diary/user-article.asp?dn=1733

        在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 与 netStreamsend 这个 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 并执行指定 Clien t端 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);
};

你可能感兴趣的:(method,职场,休闲)