我需要一个程序,可以创建屏幕上的预定义形状根据我通过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()上等待来自客户端的命令,但是它没有像它应该打开的那样打开图形窗口。有没有什么命令可以强制它打开或渲染
谢谢
发布于 2012-07-13 01:37:20
MOAI不会在单独的线程中运行你的脚本。阻塞调用(server:accept)或永久循环(while true do)将阻塞你的MOAI应用程序,当它永远快乐地驻留在你的脚本中时,它看起来会冻结。
所以你必须做两件事:
server:accept立即返回。检查它的返回值,看看在协程中是否有一个connection.您需要以相同的方式处理客户端,在协程循环中使用非阻塞调用。
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-13 06:52:46
设置服务器套接字的超时,因为accept是一个阻塞调用。
server:settimeout(1)发布于 2012-07-13 17:47:54
谢谢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不过,这很有帮助
https://stackoverflow.com/questions/11455771
复制相似问题