各位朋友好,我最近开始用Openresty搭建一个tcp服务器,遇到这样的问题:
local sock = assert(ngx.req.socket())
sock:settimeout(15000)
while true do
local line, err, part = sock:receive("*a")
if line then
ngx.log(ngx.ALERT, line)
ngx.say("log end!")
else
ngx.say("failed to receive a line: ", err, " [", part, "]")
break
end
ngx.sleep(1)
end
sock:close()
ngx.say("closed")
上面这段代码中,调用receive的时候会阻塞,然后客户端得到的是“failed to receive a line: timeout [abcdefg]”,其中“abcdefg”是我客户端发送的。那么问题就来了,它实际上是读的到的啊,为什么会阻塞直到timeout,为什么line为nil,为什么part是客户端发送的数据?求前辈们指导指导,谢谢哈。