Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua-resty-websocket能不能在定时器里面主动发送数据到客户端 #27

Closed
omega-pw opened this issue Sep 4, 2017 · 2 comments

Comments

@omega-pw
Copy link

omega-pw commented Sep 4, 2017

我试过用ngx.timer.every在websocket连接之后设置定时器,定时器能够正常执行,但是一到wb:send_text方法就报错,如果服务端不能主动推送数据,只能在wb:recv_frame()之后才能发送的话,就不如用简单的http请求了。
求帮助!

测试代码
local server = require "resty.websocket.server"
local wb, err = server:new{
timeout = 5000, -- in milliseconds
max_payload_len = 65535,
}
if not wb then
ngx.log(ngx.ERR, "failed to new websocket: ", err)
return ngx.exit(444)
end

ngx.timer.every(5, function (premature, wb)
ngx.log(ngx.ERR, nil == wb)
local bytes, err = wb:send_text("hello")--此处报错
if not bytes then
ngx.log(ngx.ERR, "failed to send text: ", err)
return ngx.exit(444)
end
end, wb)

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("hello")
if not bytes then
ngx.log(ngx.ERR, "failed to send text: ", err)
return ngx.exit(444)
end
end
end
wb:send_close()

@agentzh
Copy link
Member

agentzh commented Sep 4, 2017

@omega4github Please, do not use Chinese here. This place is considered English only. If you
really want to use Chinese, please join and post to the openresty (Chinese)
mailing list instead. Please see https://openresty.org/en/community.html Thanks for
your cooperation.

@agentzh agentzh closed this as completed Sep 4, 2017
@agentzh
Copy link
Member

agentzh commented Sep 4, 2017

@omega4github Regarding your question, it is officially documented: https://github.com/openresty/lua-resty-websocket#limitations You are just using the websocket object in the wrong context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants