1

我使用了 Luasocket 网站上给出的示例来尝试一下,我的目标是制作一个可以与套接字通信的 Flash 游戏。

我首先使用 telnet 运行服务器并连接到它,它工作正常,我发送的每条消息都出现在控制台上,所以我将它带到下一步并通过 AS 3 连接到它,它确实连接但服务器不会收到任何消息,即使我不断地 write() 给它。

有什么我遗漏的东西不会让 actionscript 应用程序与 lua 套接字服务器通信吗?

代码

-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
  -- wait for a connection from any client
  local client = server:accept()
  -- make sure we don't block waiting for this client's line
  client:settimeout(10)
  -- receive the line
  local line, err = client:receive()
  -- if there was no error, send it back to the client
  if not err then client:send(line .. "\n") end
  -- done with client, close the object
  client:close()
end

动作脚本:

var sock:Socket = new Socket();
sock.connect("127.0.0.1",3335);
stage.addEventListener(Event.ENTER_FRAME,test);
public function test(e:Event):void{
    sock.writeUTF("Hello world");
}
4

1 回答 1

1

client:receive() 方法的标准操作模式是“*l”,它等待输入流中的换行符返回。http://w3.impa.br/~diego/software/luasocket/tcp.html#receive

要更正此问题,请发送“Hello world\n”(假设它是 actionscript 中的正确转义字符)或在 receive() 中使用另一个参数。

于 2013-11-21T19:19:32.043 回答