Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2324990
  • 博文数量: 141
  • 博客积分: 3552
  • 博客等级: 中校
  • 技术积分: 4148
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-15 14:39
个人简介

熟悉Linux下程序设计及各种应用程序 熟悉C Language 熟悉Glusterfs、FFmpeg、CDN 系统设计,计算机图形系统设计、分布式程序设计 目前主要研究方向:流媒体

文章分类

全部博文(141)

分类: LINUX

2015-04-28 15:02:33

MonaServer 中事件(Event)的基本使用

MonaServer中,事件一般是以on开头的方法,例如

onStartonStoponConnection

Serverapp被创建的时候并且第一次执行到的时候,才会调用这个事件,这个事件的第一个参数是apppath

注意

所有的serverapp是在第一个client链接过来的时候才出发的事件,只有根app是在MonaServer程序刚开始运行的时候就创建

serverapp被卸载的时候会调用这个事件,这个事件在以下3中情况中才会出现:

  • 当对应的app下面的main.lua文件被编辑,app被重启过(也就是关闭再打开)
  • 当你删掉MonaServer对应的app
  • MonaServer被停掉时

这个事件的第一个参数是apppath

 

 

当有新的客户端连接上来时会调用这个事件,第一个参数是Client对象, 并且这个参数依赖于对应的协议(例如RTMPRTSPHTTPWebSocket均有不同)

最后你可以返回一个表发送一些信息给RTMP&RTMFP连接或者,或者重载一些配置参数:

  • timeout , 单位 秒. 可以通过配置文件进行配置

.

function onConnection(client,...)

  return {message="welcome",id=1,timeout=7}

end

ActionScript3 代码实现返回参数输出:

function onStatusEvent(event:NetStatusEvent):void {

  switch(event.info.code) {

    case "NetConnection.Connect.Success":

    trace(event.info.message); // displays "welcome"

    trace(event.info.id); // displays "1"

    break;

  }

}

可以拒绝客户端并加入错误信息:

function onConnection(client,login)

  if login ~= "Tom" then

    error("you are not Tom!")

  end

end

 

_netConnection.connect("rtmfp://localhost/","Ben")

 

function onStatusEvent(event:NetStatusEvent):void {

  switch(event.info.code) {

    case "NetConnection.Connect.Rejected":

    trace(event.info.description); // displays "you are not Tom!"

    break;

  }

}

RTMP&RTFMP中的回应信息为NetConnection.Connect.Rejected 状态事件并且关闭客户端连接. 时间信息描述区域包含了错误信息。 可以拒绝一个客户端并且不给任何错误信息, event.info.description 区域会默认包含“client rejected”.

也可以再找个事件中提交一个Client的事件,类似下面::

function onConnection(client)

 

  function client:onMessage(message)

    NOTE(client.address.." says "..message)

  end

 

end

 

Client断开链接事触发这个事件,Client参数为断开链接的Client句柄

注意

在这里你再也发送不了任何信息给客户端呢,所有的信息发送给Writer都不会有Duang

每隔两秒钟会被触发一次, 这个事件只在MonaServer的根app下面才会出现。这个可以很容易的获得并管理objects,这个是用在必要的时候,平时可以不用。

这个事件用在p2p中,没有找到约定服务时可以重定向Client搜索的peerId。通常将Client重定向到一个或多个其他MonaServer。可以返回一个地址,或多个地址,或数组形式的地址。

function onRendezVousUnknown(protocol, peerId)
  return 192.168.0.2:1935
end
function onRendezVousUnknown(protocol, peerId)
  return 192.168.0.2:1935,192.168.0.3:1935
end
addresses = {192.168.0.2:1936,192.168.0.3:1936}
function onRendezVousUnknown(protocol, peerId)
  return addresses
end

这个可以返回一个Server对象或者多个Servers的对象:

function onRendezVousUnknown(protocol, peerId)
  return mona.servers[1] -- redirect to the first server connected
end
function onRendezVousUnknown(protocol, peerId)
  return mona.servers -- redirect to all the connected servers
end

注意

当这个函数返回多个地址时,Client端会接收多个地址,并且去并行的开始尝试链接这么多个地址的Server

在返回地址(可以多个)的重定向地址时,允许客户机重定向到另一个MonaServer。返回值与onRendezVousUnknown(Protocol, Peerid)的返回值完全相同。这个是在Client链接过来的第一数据包时触发的。第一个地址参数 是客户端的地址address, path参数表示连接的路径,properties参数是一个HTTP形式的URL的连接,和attempts参数指示尝试连接的数量 (开始1和增加在每次尝试)

_netConnection.connect("rtmfp://localhost/myApplication?acceptableAttempts=2");
index=0
function onHandshake(address,path,properties,attempts)
  if attempts > properties.acceptableAttempts then
    -- This time we return all server available,
    -- and it's the client who will test what is the server the faster with parallel connection
    -- (first which answers wins)
    return mona.servers
  end
  index=index+1
  if index > mona.servers.count then index=1 end -- not exceed the number of server available
  return mona.servers[index] -- load-balacing system!
end

注意

如果不重定向的话,可以讲这个请求发给自己

function onHandshake(address,path,properties,attempts)
  return mona.servers,"again" -- redirect to the other server and my myself
end

 

 

 

 

 

 

 

阅读(13910) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

T-Bagwell2015-05-07 19:45:06

colin_lin:看了博主的文章,受益匪浅,想请教一下:1、MonaServer推流密码和观看密码如何实现;2、MonaServer如何与外部程序交互,如PHP,JAVA等;万分感谢!

1、MonaServer推流密码和观看密码如何实现;
   这个一般用防盗链方式去做的,在URL中做参数,key=value方式的,然后onConnection中处理
2、MonaServer如何与外部程序交互,如PHP,JAVA等;万分感谢!
   外部交互,可以考虑用onManage(),p2p的话onRendezVousUnknown比较有用

回复 | 举报

colin_lin2015-05-03 07:12:28

看了博主的文章,受益匪浅,想请教一下:1、MonaServer推流密码和观看密码如何实现;2、MonaServer如何与外部程序交互,如PHP,JAVA等;万分感谢!