I'm trying to connect a Windows computer game which uses Lua to external server written in Ruby and hosted on my Macbook on the same network.
This is Lua code executed at the beginning in-game
socket = require("socket")
udp = socket.udp()
udp:setpeername('192.168.0.17', 9100) -- This is my laptop
udp:settimeout(0.001)
This Lua code executed on every simulation step:
udp:send("Simulation step")
message = udp:receive()
print(message)
An this is Ruby server:
i = 0
Socket.udp_server_loop(9100) {|msg, msg_src|
puts i.to_s + ": " + msg + " | " + msg_src.inspect.to_s
i += 1
msg_src.reply i.to_s + ": reply from server"
}
So far so good, game receives "reply from server" correctly, prints it and I can see in Ruby console on my laptop:
19: Simulation step | #<Socket::UDPSource: 192.168.0.80:55697 to 192.168.0.17:9100>
But I'd like to do it a little more asynchronous - I want to receive data continuously and only send something back from time to time. I figured out that with UDP I don't have to worry about connections - just send message and it goes through.
I tried this in Ruby:
require 'socket'
s = UDPSocket.new
while 1 do
s.send "hi", 0, "192.168.0.80", 9100
end
This is not being received by Luasocket. Increasing timeout to 1 second didn't help. Changing port to the one which Windows used to send UDP packets (55697) didn't work.
Firewalls are disabled on both machines.
It's not feasible to just send msg_src.reply
when I have something to send, it will usually spend some time doing calculations it will not reply on time (0.001 second before timeout)
How to do this with UDP? Or maybe it's not possible and I should try with TCP?