Corona客户端,我可以接收无限的消息。

我在 Corona SDK 上开发了一个客户端,但是遇到一个问题: 我可以连接到我们的服务器并接收服务器发送的第一条消息。我也可以回复服务器。但之后,我开始收到空字符串。我怀疑我的客户端代码每次读取 '\0' 字符,但我不确定。以下是我的客户端代码:

(服务器每 5 秒发送一条消息“{ id = 1000 }”)

```

点赞
用户2186639
用户2186639

我改变了阅读例程,它运行得很好。以下是答案:

        for i,v in ipairs(input) do  -------------

            local got_something_new = false
            while  true  do
                skt, e, p = v:receive(1)
                if skt      then
                    if skt ~= '\0' then
                        self.buffer = self.buffer..skt
                    else
                        got_something_new = true
                        self.buffer = "__JSON__START__"..self.buffer.."__JSON__END__"
                    end
                end
                if p        then
                    if p ~= '\0' then
                        self.buffer = self.buffer..p;
                    else
                        got_something_new = true
                        self.buffer = "__JSON__START__"..self.buffer.."__JSON__END__"
                    end
                end
                if not skt  then break; end
                if e        then print( "ERROR: ", e ); break; end
            end

            -- 现在,检查缓冲区中是否存在消息...
            while got_something_new do  --  这是为了处理缓冲区中存储多个消息的情况
                local start = string.find(self.buffer,'__JSON__START__')
                local finish = string.find(self.buffer,'__JSON__END__')
                if (start and finish) then -- 找到一条消息!
                    local message = string.sub( self.buffer, start+15, finish-1)
                    self.buffer = string.sub( self.buffer, 1, start-1 )  ..   string.sub(self.buffer, finish + 13 ) -- 从缓冲区中剪切我们的消息
                    self.callback(  message  )
                else
                    break
                end
            end
        end
2013-05-17 10:29:09