FreeSWITCH之lua脚本事件订阅

FreeSWITCH中通过订阅事件,我们能获取到各种实时信息,进而可以对通话进行精确的控制。在lua中我们既能方便地订阅各种事件,也能自己生成所需的事件。


相关接口简要说明

freeswitch.Event创建一个事件,然后添加内容,最后发出

  • event:addBody

  • event:addHeader

  • event:fire

freeswitch.EventConsumer:订阅一个事件,获取内容

  • event:getBody

  • event:getHeader

  • event:getType

  • event:serialize

序列化输出获取的事件内容

-- Print as text

io.write(params:serialize());

io.write(params:serialize("text")); 

-- Print as JSON

io.write(params:serialize("json"));

订阅事件后,通过pop获取事件:

con = freeswitch.EventConsumer("<event_name>"[,"<subclass type>"]); 

-- pop() returns an event or nil if no events

con:pop() 

-- pop(1) blocks until there is an event

con:pop(1) 

-- pop(1,500) blocks for max half a second until there is an event

通过bind订阅所需事件

con = freeswitch.EventConsumer();

con:bind("RELOADXML");

con:bind("SHUTDOWN");

con:bind("CUSTOM", "multicast::event");


事件创建

local event = freeswitch.Event("custom", "Xugd::Quit")

event:addHeader("myKey", "MyValue")

event:fire()


事件订阅

订阅指定事件,获取并输出,直到有退出事件

local con = freeswitch.EventConsumer();
con:bind("SHUTDOWN")
con:bind("CUSTOM", "Xugd::MyStatus")
con:bind("CUSTOM", "Xugd::Quit")

while true do
    local evt = con:pop(1)
    local eType = evt:getType() 
    freeswitch.consoleLog("DEBUG", "Get Event: " .. eType .. ", " .. type(eType) .. "\n")
    if eType == "SHUTDOWN" then
        freeswitch.consoleLog("INFO", "SHUTDOWN: To Quit\n")
        break
    end
    
    if eType == "CUSTOM" then 
        local eClass = evt:getHeader("Event-Subclass")      
        freeswitch.consoleLog("DEBUG", "Event-Subclass: " .. eClass .. "\n")
        if eClass == "Xugd::Quit" then
            freeswitch.consoleLog("INFO", "Xugd::Quit: To Quit\n")
            break
        end
        
        if eClass == "Xugd::MyStatus" then
            local status= evt:getHeader("MyStaus")
            .....           
            freeswitch.consoleLog("INFO", "Xugd::MyStatus: " .. status .. "\n") 
        end
    end -- if-type  

end -- while

猜你喜欢

转载自blog.csdn.net/alwaysrun/article/details/81610660