Lua 电晕客户端,我收到无限的信息
我在Corona SDK上开发了客户端,但我有一个问题: 我可以连接到服务器并从服务器接收第一条消息。我也可以给你回信。但之后我开始接收空字符串。我怀疑我的客户端代码每次都读取“\0”字符,但我不确定。这是我的客户代码: (服务器每5秒钟发送一条消息“{id=1000}”)Lua 电晕客户端,我收到无限的信息,lua,client-server,coronasdk,tcpclient,Lua,Client Server,Coronasdk,Tcpclient,我在Corona SDK上开发了客户端,但我有一个问题: 我可以连接到服务器并从服务器接收第一条消息。我也可以给你回信。但之后我开始接收空字符串。我怀疑我的客户端代码每次都读取“\0”字符,但我不确定。这是我的客户代码: (服务器每5秒钟发送一条消息“{id=1000}”) 我改变了阅读习惯,效果很好。答案如下: for i,v in ipairs(input) do ------------- local got_something_new = fa
我改变了阅读习惯,效果很好。答案如下:
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
-- now, checking if a message is present in buffer...
while got_something_new do -- this is for a case of several messages stocker in the buffer
local start = string.find(self.buffer,'__JSON__START__')
local finish = string.find(self.buffer,'__JSON__END__')
if (start and finish) then -- found a message!
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 ) -- cutting our message from buffer
self.callback( message )
else
break
end
end
end
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
-- now, checking if a message is present in buffer...
while got_something_new do -- this is for a case of several messages stocker in the buffer
local start = string.find(self.buffer,'__JSON__START__')
local finish = string.find(self.buffer,'__JSON__END__')
if (start and finish) then -- found a message!
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 ) -- cutting our message from buffer
self.callback( message )
else
break
end
end
end