The main server file:
Code: Select all
function love.load()
socket = require("socket")
udp = socket.udp()
udp:settimeout(0)
udp:setsockname('*', 12345)
err = love.thread.getChannel("err")
sendlist = love.thread.getChannel("sendlist")
thread1 = love.thread.newThread("thread.lua")
thread1:start()
users = {}
end
function love.update()
data, ip, port = udp:receivefrom()
if data then
kind, name = data:match("(%S*) (%S*)")
if kind == "connect" then
u = {
name = name,
ip = ip,
port = port,
}
table.insert(users, u)
elseif kind == "disconnect" then
for i,v in pairs(users) do
if v.name == name and
v.ip == ip and
v.port == port then
table.remove(users, i)
end
end
else
for i,v in pairs(users) do
--if v.name ~= name then
data = string.gsub(data, " ", ";")
sendlist:push(string.format("%s %s %s", data, v.ip, v.port))
--end
end
end
end
end
function love.draw()
love.graphics.print(tostring(thread1:isRunning()))
data = err:pop()
if data then
if data then
e1, e2 = data:match("(%S*) (%S*)")
end
love.graphics.print(tostring(e1.." "..e2), 0, 15)
end
end
Code: Select all
socket = require("socket")
udp = socket.udp()
udp:settimeout(0)
udp:setsockname('*', 12345)
err = love.thread.getChannel("err")
sendlist = love.thread.getChannel("sendlist")
while true do
data = sendlist:pop()
if data then
data, ip, port = data:match("(%S*) (%S*) (%S*)")
data = string.gsub(data, " ", ";")
e1, e2 = udp:sendto(data, ip, port)
err:push(string.format("%s %s", e1, e2))
end
end
Code: Select all
function love.load()
math.randomseed(os.time())
socket = require("socket")
udp = socket.udp()
udp:settimeout(0)
udp:setpeername("localhost", 12345)
name = math.random()
udp:send(string.format("%s %s", "connect", name))
end
function love.update()
udp:send(string.format("%s %s %s", "update", name, "Hello,world!"))
data = udp:receive()
if data then
kind, name = data:match("(%S*) (%S*)")
if kind == "update" then
kind, name, d = data:match("(%S*) (%S*) (%S*)")
end
end
end
function love.draw()
love.graphics.print(tostring(d))
end
function love.quit()
udp:send(string.format("%s %s", "disconnect", name))
end