【问题标题】:Lua Socket Connection For Corona SDKCorona SDK 的 Lua 套接字连接
【发布时间】:2013-11-18 20:06:00
【问题描述】:

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

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

【问题讨论】:

  • 呃...在我看来,这个问题太宽泛了...您应该先阅读一些有关编写公共 API 和/或 RFC 的内容...

标签: lua coronasdk luasocket


【解决方案1】:

你是幸运日^^ 我花了很多时间找到那种教程,但真的很痛苦,所以这是我的客户端库。它使用 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

【讨论】:

  • 当我们使用这个库的时候,sockets需要哪些函数呢?
  • 所有功能都可能对您有所帮助。您应该将您的客户端创建为 client = clientLib.newConnection({server="url",port="port",callback="callbackfunction"}) 然后您可以使用其他功能
【解决方案2】:

【讨论】:

    猜你喜欢
    • 2013-06-04
    • 2019-10-11
    • 1970-01-01
    • 2020-08-07
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2013-03-23
    相关资源
    最近更新 更多