Networking example
A simple example of networking, ripped straight from the brok[en]gine test suite.
Client
local has_ponged = false
local socket = net.newsocket(net.PAIR)
socket:bind("tcp://127.0.0.1:5555")
function socket:receive(stream)
print("socket receive triggered")
has_ponged = true
local message = stream:readstring()
assert(message == "ping")
socket:send(function(stream2)
stream2:writestring("pong")
end)
end
local i = os.time()
function GAME.tick()
if has_ponged then
GAME.exit()
end
if i - os.time() > 5 then
error("Failed")
end
end
Server
local socket = net.newsocket(net.PAIR)
socket:connect("tcp://127.0.0.1:5555")
function socket:receive(stream)
local message = stream:readstring()
assert(message == "pong")
end
socket:send(function(stream)
stream:writestring("ping")
end)