运行时 addeventlistener 使用 Corona 减慢 Lua 中的应用程序显示速度



从下面的代码中,如何在不中断应用程序的显示部分的情况下使运行时侦听器运行?目前,当我使用运行时侦听器时,显示速度变慢。仅供参考,我是Lua/Corona的新手,也是堆栈溢出的新人。

function scene:createScene( event )
        group2 = scrollView.new{ top=topBoundary, bottom=bottomBoundary }
        myImage = display.newImage("imgbg1.png")
        myImage.isVisible =  true
        group2:insert(myImage)
        local yval = 120
        socket,err=socket1.connect("host", port)
        while not err do
            socket:settimeout(1)
            socket:send("runcommandrn")
            repeat
              line, err = socket:receive()
              if line then 
                no = no + 1
                -- [[ get only the main properties ]] --
                if no >= 5 then 
                reply = reply .. line
                end
              end
            until err
            t1 = reply
            loadnsave:saveToJson(t1,filename)
            tsw = loadnsave:loadData(filename)
            local i = 0
            for k, v in pairs(tsw) do
                i = i + 1
                print("n" .. k , v )
                locy = locy + 20
                imgBase[i] = display.newImage("image.png")
                imgBase[i].x = locx
                imgBase[i].y = locy
                group2:insert(imgBase[i])
                sw[i] = swfunc(tsw[k],k,locy,i)
                group2:insert(sw[i])
                locy = locy + 40 + iHeight
            end
            inity = 1
            initw = 1
        end
    Runtime:addEventListener("enterFrame", swlistener)
end
function swlistener(event)
    local reply2 = ""
    repeat
      line2, err2 = socket:receive()
      if line2 then 
        reply2 = reply2 .. line2
      end
    until err2
end

如果我没记错的话,这与运行时侦听器无关。它的套接字连接可能会减慢应用程序的速度。

我不是100%确定(它会冻结屏幕吗?如果是这样,那就是问题所在)如果套接字连接确实有问题,您可以执行以下操作之一。

  • 使用异步方法 network.request 如果您的要求是 GET/POST 请求
  • 禁用套接字的所谓内格尔算法 socket:setoption(tcp-nodelay,true)

我已经尝试了如下@SatheeshJM,但它不起作用

socket,err=socket1.connect("host", port)
socket:setoption("tcp-nodelay",true)

我通过设置较小的值(如 0.01)的超时来找到解决方案

socket:settimeout(0.01)

现在插座和显示器工作正常

但我认为我需要稍微改变一下我的 swlistener 功能。

相关内容

最新更新