Page 1 of 1
LUBE help
Posted: Sun Dec 23, 2012 9:43 pm
by raen79
Hey guys, I don't really understand how LUBE works, can someone please give me a very simple code for a client and a server. A server that simply receives text that the client sends it, that would be incredibly nice. I am so desperate I spent all day trying to understand this...
Re: LUBE help
Posted: Mon Dec 24, 2012 3:02 pm
by raen79
Up ?
Re: LUBE help
Posted: Tue Dec 25, 2012 2:16 pm
by Roland_Yonaba
Hi,
I am not a specialist of that networking library, but I can redirect you to that very complete post Technocat did a long time ago on these forums.
See
here.
Hope it helps.
Otherwise, you can still give more details on what want to accomplish, and maybe post a *.love showing what you come up with.
Re: LUBE help
Posted: Wed Dec 26, 2012 3:57 pm
by raen79
I had already seen this thread, but they are all outdated examples, so they don't work with the new love 0.8.0 or the new LUBE. Since I could not find a tutorial, I was hoping for a tiny example just to see how it works, instead of a tutorial you know
Re: LUBE help
Posted: Fri Dec 28, 2012 2:29 am
by Karai17
This is a basic server
Code: Select all
require "LUBE.LUBE"
port = 12345
connection = lube.tcpServer()
connection.handshake = "handshake"
connection:setPing(true, 6, "ping\n")
connection.callbacks.recv = function(d, id) recv(d, id) end
connection.callbacks.connect = function(id) connect(id) end
connection.callbacks.disconnect = function(id) disconnect(id) end
connection:listen(port)
print('Server started on port: ' .. tostring(port))
--[[
Client Connects to Server
clientId = Unique client ID
]]--
function connect(clientId)
print('Client connected: ' .. tostring(clientId))
end
--[[
Client Disconnects from Server
clientId = Unique client ID
]]--
function disconnect(clientId)
print('Client disconnected: ' .. tostring(clientId))
end
--[[
Receive Data from Client
data = Data received
clientId = Unique client ID
]]--
function recv(data, clientId)
print('Client data received from: ' .. tostring(clientId) .. ' containing: ' .. data)
end
and here is a basic client
Code: Select all
require "LUBE.LUBE"
host = "localhost"
port = 12345
connection = lube.tcpClient()
connection.handshake = "handshake"
connection:setPing(true, 2, "ping\n")
connection.callbacks.recv = function(d) recv(d) end
if connection:connect(host, port, true) then
print('Connect to ' .. host .. ': ' .. port)
end
--[[
Receive Data from Server
data = Data received
]]--
function recv(data)
print('Server data received: ' .. data)
end
To send data back and forth, you'd just use
connection:send(data) from either server or client. If you are sending from the server, you can use
connection:send(data, clientId) to send to a specific client. Omitting that will send the data to every client.
Don't forget to add
connection:update(dt) to your love.update(dt) function or else the callbacks will not fire.