ngx_lua_websocket server實現

aa1215018028發表於2017-12-28
local server = require"resty.websocket.server"
local wb,err = server:new{
    timeout = 10000,
    max_payload_len = 65535,
}
if not wb then
    ngx.log(ngx.ERR,"failed to create websocket server",err)
    return ngx.exit(444)
end
local data,typ,err = wb:recv_frame()
if not data then
    if not string.find(err,'timeout',1,true) then
        ngx.log(ngx.ERR,"failed to receive a frame1",err)
        return ngx.exit(444)
    end 
end
if typ == "clese" then
    local code = err 
    local bytes, err = wb:send_close(1000,"enough,enough!")
    if not bytes then
        ngx.log(ngx.ERR,"failed to send close frame,",err)
        return
    end 
    ngx.log(ngx.INFO,"closing with status code",code," with message  ",data)
    return
end

相關文章