0

コロナ SDK の Lua Sockets の使用方法を知っている人はいますか? デバイスを使用して 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 に答える