Вы в свой счастливый день ^^ Я потратил много времени, чтобы найти этот учебник, но это было очень больно, так что вот моя клиентская библиотека для вас. Он использует TCP/IP. Наслаждайтесь!
--------------------
-- This library created to handle TCP/IP connections on client side for Corona with Lua language.
-- @Author : Doğancan Arabacı
--------------------
module(..., package.seeall)
local socket = require("socket")
function newConnection(params)
params = params or {}
if (not params.server or not params.port) then
myPrint("SERVER OR PORT NOT SPECIFIED" );
return a
end
local self = {}
self.buffer = ""
self.server = params.server
self.port = params.port
self.isOpen = false
function self:start(params)
self.callback = params.callback
self.sock, err = socket.connect(self.server, self.port)
if (self.sock == nil) then
myPrint("COULDN'T CONNECT TO SERVER (self:start): ", err)
return false;
else
myPrint("Connected.")
end
self.isOpen = true
self.sock:setoption("tcp-nodelay", true) -- disable Nagle's algorithm for the connection
self.sock:settimeout(0)
self.sock:setoption("keepalive", true)
return true
end
function self:close()
myPrint( "Closing server connection")
if self.sock then
self.sock:close()
self.sock = nil
self.buffer = ""
end
end
function self:reconnect()
if (not self.callback) then
myPrint("NO CALLBACK FUNCTION ON RECONNECT ATTEMPT")
return false
end
myPrint("RECONNECTING TO SERVER")
self:start({ callback = self.callback})
end
function self:isActive()
if self.sock == nil then
return false
else
return true
end
end
function self:send(message)
if (self.sock == nil) then
myPrint("SERVER CONNECTION LOST")
self:reconnect()
return false;
end
local send_result, err, num_byes = self.sock:send(json.encode(message) ..'\0' )
if (send_result == nil) then
myPrint("ERROR TRYING TO SEND MESSAGE TO SERVER: "..err..' SENT '..num_byes..' BYTES');
if (err == 'closed') then self:reconnect() end
return false;
else
myPrint("Message sent : "..json.encode(message).." - "..send_result)
end
return true
end
function self:enterFrame()
local input,output = socket.select({ self.sock }, nil, 0) -- this is a way not to block runtime while reading socket. zero timeout does the trick
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 got_something_new == true then break end
if not skt then break; end
if e then myPrint("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
self.buffer = ""
break
end
end
end
end
Runtime:addEventListener('enterFrame', self)
return self
end
эээ ... вопрос, на мой взгляд, это тааак широкий ... Вы должны прочитать что-то о написании публичных API-интерфейсов и/или RFC первый .. – Kamiccolo