openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.3k stars 2.03k forks source link

how to use receiveany? #1773

Open piaoxuebingfeng opened 4 years ago

piaoxuebingfeng commented 4 years ago

local sock = ngx.req.socket(true)

local maxlen = 16*1024 local data = nil local err = nil local receivenum = 0

sock:settimeout(1900)

sock:settimeouts(2000, 2000, 2000) -- one second timeout for connect/read/write local data, err = sock:receiveany(1024) -- read any data, at most 10K if not data then ngx.say("failed to read any data: ", err) return end ngx.log(ngx.ERR,"read data:"..tostring(#data)) ngx.say("successfully read: ", data)

when i use receiveany API ,i got error: 2020/08/24 09:17:51 [error] 17713#0: *5 lua entry thread aborted: runtime error: /usr/local/nginx/conf/receiveany.lua:13: attempt to call method 'receiveany' (a nil value)

i want to know how can i use receiveany API ?

Ymir1711 commented 4 years ago

https://github.com/openresty/lua-nginx-module/commit/b5ffb11fe1cdedd9923cb4fe0211217fbc08b28a https://github.com/openresty/openresty/releases Not implemented in v1.15.8.3, try v1.17.8.1rc1. or

local sock, err = ngx.req.socket() if not sock then ngx.say("ngx.req.socket err: ", err) return end sock:settimeout(2000) local body = "" repeat local data, err, partial = sock:receive(1024) if data then body = body..data elseif partial then body = body..partial end if err then break end until (not data and not partial) ngx.say("Body Length: ", string.len(body))

doujiang24 commented 4 years ago

@Kael1117 the formal release v1.17.8.2 is out. http://openresty.org/en/ann-1017008002.html

piaoxuebingfeng commented 3 years ago

thanks all, i test receiveany() api ok,in openresty-1.19.3.1rc0.