Page 1 of 1

LuaSocket

Posted: Thu May 21, 2015 11:00 pm
by EliterScripts
Can someone give me a server-side code examples, of LuaSocket (like, code that is ran ON the server)?

Thanks! :D

Re: LuaSocket

Posted: Fri May 22, 2015 1:30 am
by Zleub
Hi !

This is truncated code (no clients or such) but here's a sample of server-side code. It's from an old practice : Github link. Do not hesitate to ask questions if you have so.

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


Re: LuaSocket

Posted: Fri May 22, 2015 1:39 am
by Positive07
http://w3.impa.br/~diego/software/luaso ... n.html#tcp

http://w3.impa.br/~diego/software/luasocket/tcp.html

https://love2d.org/wiki/socket

https://love2d.org/wiki/Tutorial:Networking_with_UDP If you are using UDP instead of TCP

You should also take a look at ENET

https://love2d.org/wiki/lua-enet

This old post has some server and client example for AN OLDER VERSION of LÖVE (You can still look at the code)
viewtopic.php?f=4&t=8931#p55200