1

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

问题是它没有按应有的方式创建任何图形或打开窗口..

require "socket"
require "mime"
require "ltn12"

host = "localhost"
port = "8080"
server, error = socket.bind(host, port)
if not server then print("server: " .. tostring(error)) os.exit() end


screen=MOAISim.openWindow ( "test", 640, 640 )

viewport = MOAIViewport.new (screen)
viewport:setSize ( 640, 640 )
viewport:setScale ( 640, 640 )

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

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

fillSquare(0,64,64, 0,0,255)
end

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


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


while 1 do
   print("server: waiting for client command...")
   control = server:accept()
    command, error = control:receive()
    print(command,error)
    error = control:send("hi from Moai\n")

end

它在 control = server:accept() 等待来自客户端的命令,但它没有按应有的方式打开图形窗口。是否有任何命令强制它打开或渲染

谢谢

4

3 回答 3

2

MOAI 不会在单独的线程中运行您的脚本。阻塞调用 ( server:accept) 或永久循环 ( while true do) 将阻塞您的 MOAI 应用程序,并且它看起来会冻结,而它会永远快乐地坐在您的脚本中。

所以你必须做两件事:

  1. 使用非阻塞调用。在这种情况下,您需要将服务器的超时设置为 0。这会server:accept立即返回。检查它的返回值,看看你是否有连接。
  2. 将您的 while 循环放入协程中,每次迭代产生一次。

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

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

  client:settimeout(0) -- make client socket reads non-blocking

  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) -- make server:accept call non-blocking

    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-12T17:37:19.830 回答
0

谢谢Mud ...我发现在你回复之前,下面的协程可以工作

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: closing connection...")
                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-13T09:47:54.187 回答
0

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

server:settimeout(1)
于 2012-07-12T22:52:46.357 回答