0

有谁知道如何为 Corona sdk 使用 Lua 套接字,我想使用设备将 API 传递给服务器并将数据从服务器接收回设备。我只找到http://appcodingeasy.com/Gideros-Mobile/Using-LuaSocket-in-Gideros,它适用于 Gideros SDK。

任何帮助,将不胜感激。谢谢你。

4

2 回答 2

6

你是幸运的一天^^ 我花了很多时间找到那种教程,但真的很痛苦,所以这里是我的客户端库。它使用 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
于 2013-11-08T11:50:02.477 回答
0

LuaSocket 如何工作的另一个例子: https ://github.com/Overtorment/NoobHub/blob/master/client/lua-corona/noobhub.lua

于 2013-12-31T16:51:51.000 回答