Luasocket服务器和ActionScript

我在 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

Actionscript:

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");
}
点赞
用户3019030
用户3019030

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

为了纠正这个问题,可以发送 "Hello world\n"(假设这是 actionscript 中正确的转义字符),或者在 receive() 中使用另一个参数。

2013-11-21 19:19:32