我需要一个程序,可以根据我通过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()但它没有打开它应该的图形窗口..是否有任何命令强制它打开或渲染
谢谢
答案 0 :(得分:2)
MOAI不会在单独的线程中运行脚本。阻止调用(server:accept
)或永久循环(while true do
)将阻止您的MOAI应用程序,它会在您的脚本中永久存在时冻结。
所以你必须做两件事:
server:accept
立即返回。检查它的返回值,看看你是否有连接。你需要以同样的方式处理客户端,在协程循环中使用非阻塞调用。
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)
答案 1 :(得分:0)
设置服务器套接字的超时,因为accept是阻塞调用。
server:settimeout(1)
答案 2 :(得分:0)
谢谢泥...我发现在你回复之前,以下的协同工作
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
虽然
非常有帮助