Moai: 通过套接字响应命令的图形

我需要一个程序,可以根据我通过TCP发送给它的命令,在屏幕上创建预定义的形状。 我正在尝试监听一个端口,以便我可以使用它们。在等待命令(通过网络)之前,我已经有了创建正方形所需的命令(我计划通过网络命令更改其属性)

问题是它没有创建任何图形或打开窗口,就像应该的那样...

需要 "socket"
需要 "mime"
需要 "ltn12"

主机 = "localhost"
端口 = "8080"
服务器,错误= socket.bind(主机,端口)
如果没有服务器,则打印(“服务器:”.. tostring(error)) os.exit()结束

screen=MOAISim.openWindow(“test”,640640)

视口= MOAIViewport.new(screen)
视口:setSize(640640)
视口:setScale(640640)

层= MOAILayer2D.new()
layer:setViewport ( viewport )
 MOAISim.pushRenderPass ( layer )

函数fillSquare(x,y,radius,red,green,blue)
a = red / 255
b = green / 255
c = blue / 255
MOAIGfxDevice.setPenColor(a,b,c)--绿色
MOAIGfxDevice.setPenWidth(2)
MOAIDraw.fillCircle(x,y,半径,4-- x,y,r,steps
end
函数onDraw()

fillSquare(0646400255)
端

scriptDeck = MOAIScriptDeck.new()
scriptDeck:setRect(-64-646464)
scriptDeck:setDrawCallback(onDraw)

prop = MOAIProp2D.new()
prop:setDeck(scriptDeck)
layer:insertProp(prop)

而 1print(“服务器:等待客户端命令...”)
控制=服务器:接受()
    命令,错误=控制:接收()
    print(command,error)
    错误=控制:发送(“来自Moai的嗨\n”)

端

它正在等待来自客户端的命令,在control = server:accept() 但它没有打开图形窗口,就像应该的那样.. 是否有命令来强制它打开或渲染

谢谢

点赞
用户501459
用户501459

MOAI 不会在一个独立的线程中运行你的脚本。一个阻塞调用(server:accept)或者无限循环(while true do)会阻塞你的 MOAI 应用程序,导致它看上去被冻结在你的脚本中。因此你需要做两件事情:

1.使用非阻塞调用。在这种情况下,你需要将服务器的超时时间设为0,这会使server:accept立即返回。检查它的返回值以查看是否连接成功。

2.将你的 while 循环放在一个协程中,每次迭代时 yielding。

你需要以同样的方式处理客户端,使用协程循环中的非阻塞调用。

function clientProc(client)
  print('client connected:', client)

  client:settimeout(0) -- 使客户端套接字读取变为非阻塞

  while true do
    local command, err = client:receive('*l')
    if command then
      print('received command:', command)
      err = client:send("hi from Moai\n")
    elseif err == 'closed' then
      print('client disconnected:', client)
      break
    elseif err ~= 'timeout' then
      print('error: ', err)
      break
    end
    coroutine.yield()
  end
  client:close()
end

function serverProc()
    print("server: waiting for client connections...")

    server:settimeout(0) -- 使 server:accept 调用变为非阻塞

    while true do
        local client = server:accept()
        if client then
            MOAICoroutine.new():run(clientProc, client)
        end
        coroutine.yield()
    end
end

MOAICoroutine.new():run(serverProc)
2012-07-12 17:37:19
用户1072825
用户1072825

设置服务器套接字的超时时间,因为accept是一个阻塞调用。

server:settimeout(1)
2012-07-12 22:52:46
用户1512236
用户1512236

感谢 Mud...在你回复之前我已经找到了以下 coroutine 的作用:

function threadFunc()
    local action
    while 1 do

    stat, control = server:accept()
    --print(control,stat)
    while 1 do
        if stat then
            command, error = stat:receive()
            print("Comm: ", command, error)
            if command then

                stat:close()
                print("server: 关闭连接...")
                break
            else
                break
            end
            --[[
          error = stat:send("hi")
          if error then
              stat:close()
              print("server: closing connection...",error)
              break
          end  ]] --
        else
            break
        end

    end
    coroutine.yield()
end
end

不过,那确实是非常有帮助的。

2012-07-13 09:47:54