stream-lua-nginx-module调用socket:receive返回time out

203 views
Skip to first unread message

caijianb...@gmail.com

unread,
Nov 16, 2016, 6:38:11 AM11/16/16
to openresty
各位朋友好,我最近开始用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是客户端发送的数据?求前辈们指导指导,谢谢哈。

DeJiang Zhu

unread,
Nov 17, 2016, 9:47:27 PM11/17/16
to open...@googlegroups.com
Hello

在 2016年11月16日 下午7:38, <caijianb...@gmail.com>写道:
各位朋友好,我最近开始用Openresty搭建一个tcp服务器,遇到这样的问题:

local sock = assert(ngx.req.socket())

sock:settimeout(15000)

while true do  

    local line, err, part = sock:receive("*a")

很明显你并没有仔细看文档

  • '*a': reads from the socket until the connection is closed. No end-of-line translation is performed;

    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是客户端发送的数据?求前辈们指导指导,谢谢哈。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Reply all
Reply to author
Forward
0 new messages