2012-07-12 2 views
1

TCP를 통해 보내는 명령에 따라 화면에 미리 정의 된 모양을 만들 수있는 프로그램이 필요합니다. 나는 포트를 경청하고 그들을 사용할 수 있도록 노력하고있다. 네트워크를 통해 명령을 기다리기 전에 네트워크 명령을 통해 속성을 변경하려고하는 사각형이 필요합니다.Moai : 소켓을 통해 명령에 반응하는 그래픽

문제점은 그래픽을 생성하거나 창이 열리지 않아야한다는 것입니다. ..

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 

그것은 = 서버 컨트롤에 클라이언트에서 명령의 대기 : (동의)하지만 ... 그것을 열거 나 렌더링하기 위해 강제로 모든 명령이인가되어야로 그래픽 창을 개방하지 않습니다

감사합니다.

답변

2

OAI는 별도의 스레드에서 스크립트를 실행하지 않습니다. 차단 호출 (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) 
+0

이 코 루틴 코드가 –

0

수신이 차단 호출이기 때문에 서버 소켓의 시간 초과를 설정하십시오.

server:settimeout(1) 
+0

그 동안 작동하는 당신에게 진흙을 감사하지만 루프가 블록 MOAI를 중지합니다. – Mud

0

감사 진흙 ... 난 u는 대답하기 전에 그래서 다음과 같은 코 루틴이 작동하는 것을 발견 매우 도움이되었다

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 

+0

MOAI 포럼에서 찾은 게시물도 내 것입니다. :) – Mud