Hello,
> Hello!
>
> On Sat, Jul 19, 2014 at 6:33 AM, Noe wrote:
>> pls, can you explain me how to implement to the this server example -> https://medium.com/technology-and-programming/websockets-with-openresty-1778601c9e05 a function which will send a data to the client in periodical intervals (lets say 60s), but be not blocked for client request?
>>
>
> Just use ngx.sleep(60) between your successive send_xxxx() calls:
>
I’ve tried it before, but it blocked my cliend request for the sleep interval. Maybe I placed it wrong.
> https://github.com/openresty/lua-nginx-module#ngxsleep
>
> If your request handler already has its own processing logic, then you
> can create a "light thread" running aside:
>
> https://github.com/openresty/lua-nginx-module#ngxthreadspawn
>
> You will need the full-duplex cosocket support in the latest ngx_lua (0.9.10+).
>
I’ll check this option.
I created new function notification_send which increment timer and chek it next iteration if send data or not. It is OK but the send interval is not exact 60s, because of timeout setting of ws server - its vary between 6s1-65s. But for now, this solution is ok for me.
local server = require "resty.websocket.server"
my_start_time = os.time()
my_notify_interval = 60
function notification_send(wb)
local current_time = os.time()
if my_start_time+my_notify_interval < current_time then
local bytes, err = wb:send_text("hi" .. " current interval " .. current_time - my_start_time)
my_start_time = my_start_time + my_notify_interval
end
end
local wb, err = server:new{
timeout = 5000,
max_payload_len = 65535
}
if not wb then
ngx.log(ngx.ERR, "failed to new websocket: ", err)
return ngx.exit(444)
end
while true do
local data, typ, err = wb:recv_frame()
if wb.fatal then
ngx.log(ngx.ERR, "failed to receive frame: ", err)
return ngx.exit(444)
end
if not data then
local bytes, err = wb:send_ping()
if not bytes then
ngx.log(ngx.ERR, "failed to send ping: ", err)
return ngx.exit(444)
end
elseif typ == "close" then break
elseif typ == "ping" then
local bytes, err = wb:send_pong()
if not bytes then
ngx.log(ngx.ERR, "failed to send pong: ", err)
return ngx.exit(444)
end
elseif typ == "pong" then
ngx.log(ngx.INFO, "client ponged")
elseif typ == "text" then
local bytes, err = wb:send_text(data)
if not bytes then
ngx.log(ngx.ERR, "failed to send text: ", err)
return ngx.exit(444)
end
end
notification_send(wb)
end
wb:send_close()
thank you for your time and greate openresty as well ;)
regards
noe
> Regards,
> -agentzh.