FCS编程之Application对象

     Application对象可以让你接受或拒绝用户端的连接请求,注册客户端相应事件的类和方法等。一般在服务器端的main.asc文件中定义相关事件。
服务器端的程序由下列事件程序组成:
1 : application.onAppStart = function(){
2 : //应用程序初次启动时,要执行的程序
3 : //并且只会被执行一次
4 : }


1 : application.onConnect = function(){
2 : //每当有用户链接到此应用程序时,要执行的程序
3 : //可以在这里读取用户端的数据
4 : //建立可以让客户端执行的方法
5 : //或者呼叫客户端的自定义方法等
6 : }

如果使用communication server组件的话,还可以使用Application.onConnectAccept 和 application.onConnectReject事件
可以查看下面的实例程序:
01 : // A user has connected
02 : application.onConnect = function( newClient, userName )
03 : {
04 : if (userName == 'admin')
05 : {
06 : newClient.adminFunc= function(param)
07 : {
08 : // some code that’s only useful to the admin
09 : newClient.myAdminProperty = param;
10 : }
11 : } else
12 : {
13 : // code for most cases
14 : }
15 : // Allow the logon
16 : application.acceptConnection( newClient );
17 : }
18 : // this part could be in a separate file
19 : // Every client (including admin) is going to have this function
20 : // (because of the way 'prototype' works).
21 : Client.prototype.commonFunction = function (myColor)
22 : {
23 : // some code
24 : // use this to refer to the client instead of newClient
25 : this.color = myColor;
26 : }

或者
01 : application.onConnect = function(clientObj,name,passwd)
02 : {
03 : // First accept the connection
04 : application.acceptConnection(clientObj);
05 : // After client is registered with the application instance
06 : // you can use 'call' method
07 : clientObj.call('onWelcome', 'You are now connected!!!');
08 : return;
09 : // Once you call acceptConnection or
10 : // rejectConnection within onConnect, return value is ignored.
11 : }
, client,name);
12 : }
13 : }
14 : // On the client side you would have the following
15 : nc = new NetConnection();
16 : nc.userDisconnects= function (name) {
17 : trace(name + 'quits');
18 : }
19 : nc.connect ('rtmp:/app_name', userName);

注意:如果要call客户端的方法,请确认已经调用application.acceptConnection以保证客户端正常连接。

1 : application.onDisConnect = function(){
2 : //当用户离线时,要执行的程序
3 : //可以在这里清楚用户的数据
4 : //和其它相关的程序
5 : }

可以参考下面的程序:
01 : // On the server side you would have the following
02 : application.onConnect = function(newClient, name)
03 : {
04 : newClient.name = name;
05 : return true;
06 : }
07 : application.onDisconnect = function(client)
08 : {
09 : for ( var i = 0; i < application.clients.length; i++)
10 : {
11 : application.clients[ i ].call('userDisconnects'


1 : application.onAppStop = function(){
2 : //应用程序结束时,要执行的程序
3 : //也就是说所有的联机用户都离线后
4 : //这里的程序会被执行
5 : }


我们看到的main.asc文件可能并没有包含上面的任何一个事件,那是因为有可能通过load方法加载了其它的组件定义,但是我们仍然可以在main.asc文件中定义我们需要的事件和方法。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值