LuaSocket
Posted: Thu May 21, 2015 11:00 pm
Can someone give me a server-side code examples, of LuaSocket (like, code that is ran ON the server)?
Thanks!
Thanks!
Code: Select all
-- Server Side
-- main.lua
socket = require 'socket'
server = require 'server'
function love.load()
server:start()
end
function love.update(dt)
server:update(dt)
end
function love.draw()
end
Code: Select all
-- Server Side
-- server.lua
local server = {}
clients = require 'clients'
function server:init()
udp = socket.udp()
udp:setsockname(arg[2], arg[3])
udp:settimeout(0)
end
function server:newClient(data, ip, port)
print(data, ip, port)
local client = clients:addClient(ip, port)
udp:sendto(client.id.." "..client.shape._center.x.." "..client.shape._center.y, ip, port)
end
function server:match(data, ip, port)
id, cmd, args = data:match("(%d*) (%a*) (.*)")
if id then
self:responde(id, cmd, args)
end
if tonumber(data) == protocol.magic then
self:newClient(data, ip, port)
end
end
function server:update(dt)
local data, msg_or_ip, port_or_nil = udp:receivefrom()
if not data then return end
if data == "dump" then
clients:dump()
end
self:match(data, msg_or_ip, port_or_nil)
end
return server