ChinaFFmpeg

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 6969|回复: 0

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

[复制链接]
发表于 2015-4-22 10:43:16 | 显示全部楼层 |阅读模式
MonaServer 中事件(Event)的基本使用
在MonaServer中,事件一般是以on开头的方法,例如
onStart、onStop、onConnection
onStart(path)
当Server的app被创建的时候并且第一次执行到的时候,才会调用这个事件,这个事件的第一个参数是app的path
注意
所有的server的app是在第一个client链接过来的时候才出发的事件,只有根app是在MonaServer程序刚开始运行的时候就创建
onStop(path)
当server的app被卸载的时候会调用这个事件,这个事件在以下3中情况中才会出现:
[C] 纯文本查看 复制代码
•    当对应的app下面的main.lua文件被编辑,app被重启过(也就是关闭再打开)
•    当你删掉MonaServer对应的app时
•    当MonaServer被停掉时

这个事件的第一个参数是app的path


onConnection(client,...)
当有新的客户端连接上来时会调用这个事件,第一个参数是Client对象, 并且这个参数依赖于对应的协议(例如RTMP、RTSP、HTTP、WebSocket均有不同)
最后你可以返回一个表发送一些信息给RTMP&RTMFP连接或者,或者重载一些配置参数:
•    timeout , 单位 秒. 可以通过配置文件进行配置
.
[AppleScript] 纯文本查看 复制代码
function onConnection(client,...)
  return {message="bbs.chinaffmpeg.com 孙悟空",id=1,timeout=7}
end

ActionScript3 代码实现返回参数输出:
[Actionscript3] 纯文本查看 复制代码
function onStatusEvent(event:NetStatusEvent):void {
  switch(event.info.code) {
    case "NetConnection.Connect.Success":
    trace(event.info.message); // displays "bbs.chinaffmpeg.com 孙悟空"
    trace(event.info.id); // displays "1"
    break;
  }
}

可以拒绝客户端并加入错误信息:
[AppleScript] 纯文本查看 复制代码
function onConnection(client,login)
  if login ~= "Tom" then
    error("you are not bbs.chinaffmpeg.com 孙悟空!")
  end
end


[Actionscript3] 纯文本查看 复制代码
_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 bbs.chinaffmpeg.com 孙悟空!"
    break;
  }
}

在RTMP&RTFMP中的回应信息为NetConnection.Connect.Rejected 状态事件并且关闭客户端连接. 时间信息描述区域包含了错误信息。 可以拒绝一个客户端并且不给任何错误信息, event.info.description 区域会默认包含“client rejected”.
也可以再找个事件中提交一个Client的事件,类似下面::
[AppleScript] 纯文本查看 复制代码
function onConnection(client)

  function client:onMessage(message)
    NOTE(client.address.." bbs.chinaffmpeg.com 孙悟空 says  "..message)
  end

end


onDisconnection(client)
当Client断开链接事触发这个事件,Client参数为断开链接的Client句柄。
注意
在这里你再也发送不了任何信息给客户端呢,所有的信息发送给Writer都不会有Duang。
onManage()
每隔两秒钟会被触发一次, 这个事件只在MonaServer的根app下面才会出现。这个可以很容易的获得并管理objects,这个是用在必要的时候,平时可以不用。
onRendezVousUnknown(protocol, peerId)
这个事件用在p2p中,没有找到约定服务时可以重定向Client搜索的peerId。通常将Client重定向到一个或多个其他MonaServer。可以返回一个地址,或多个地址,或数组形式的地址。
[AppleScript] 纯文本查看 复制代码
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的对象:
[AppleScript] 纯文本查看 复制代码
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。
onHandshake(address,path,properties,attempts)
在返回地址(可以多个)的重定向地址时,允许客户机重定向到另一个MonaServer。返回值与onRendezVousUnknown(Protocol, Peerid)的返回值完全相同。这个是在Client链接过来的第一数据包时触发的。第一个地址参数 是客户端的地址address, path参数表示连接的路径,properties参数是一个HTTP形式的URL的连接,和attempts参数指示尝试连接的数量 (开始1和增加在每次尝试)。
[AppleScript] 纯文本查看 复制代码
_netConnection.connect("rtmfp://localhost/myApplication?acceptableAttempts=2");


[AppleScript] 纯文本查看 复制代码
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

注意
如果不重定向的话,可以讲这个请求发给自己
[AppleScript] 纯文本查看 复制代码
function onHandshake(address,path,properties,attempts)
  return mona.servers,"bbs.chinaffmpeg.com 孙悟空 said again" -- redirect to the other server and my myself
end








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|Archiver|ChinaFFmpeg

GMT+8, 2024-4-27 04:09 , Processed in 0.049397 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表