简体   繁体   中英

Luasocket server and actionscript

I used an example given on the Luasocket website just to try it out, my goal was to make a flash game that'll communicate with the socket.

I run the server and connected to it using telnet at first and it worked, every message I sent appeared on the console so I took it to the next step and connected to it through AS 3 and it did connect but the server won't receive any message even though I constantly write() to it.

Is there anything I'm missing that won't let an actionscript application communicate with the lua socket server?

Code

-- 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");
}

The standard mode of operation of the client:receive() method is "*l", which waits for a new line character in the input stream to return. http://w3.impa.br/~diego/software/luasocket/tcp.html#receive

To correct this, either send "Hello world\\n" (assuming it's the correct escape character in actionscript) or use another parameter in receive().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM