Getting a server for a love2d game

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii »

Gunroar:Cannon() wrote: Tue Jan 05, 2021 7:30 pm How do I do that( use UDP instead of TCP and the quick connect(broadcast I think?) thing
https://love2d.org/wiki/Tutorial:Networking_with_UDP
https://love2d.org/wiki/socket
http://w3.impa.br/~diego/software/luasocket/udp.html
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() »

Thnx, you've been really helpful, but how do I broadcast (with sock.lua, which uses enet).

Edited: Sorry, I was getting lazy. I found it here: https://love2d.org/wiki/enet.host:broadcast. Though now the question is do I even need to broadcast or just quickly connect and disconnect?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii »

You don't need broadcast. There is no "connect/disconnect" with UDP. Think of it like sending and receiving mail. You can send mail to any IP address, and you can wait to receive mail from any IP addresses.

To get the match info, send a packet to each IP with a query code. Then, when a packet arrives with a response code + the data, update the table you store the available matches in to reflect that data.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() »

pls give a simple example.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii »

User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() »

I just got the chance to look at that tutorial in depth. So could i do something like:

Code: Select all

    --client side
    local IPs = get_matches()
    local port = 27344--random port
    upd = socket.udp()
    udp:settimeout(0)
    
    for x, i in ipairs(IPs) do
        udp:setpeername(i, port)
        udp:send("get data")
    end
    
    --somewhere in update
    local data = udp:recieve()
    if data then
        --sort data
    end
    ...

Code: Select all

    --server side
    offer_match()
    udp = socket.udp()
    udp:setsockname("*",27344)
    udp:settimeout(0)
    
    --somewhere in update
    local data = udp:recievefrom()
    if data == "get data" then
        udp:sendTo(...)--send data stuff
    end
Then I destroy it when a game starts, or is this wrong because it seems it can be done better. You said query code but I see no way to put it. Am I missing some info.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii »

On the server side, you need to store the received query's IP and port in order to send the reply back to the correct address. Something like:

Code: Select all

--server load
match_name = "casual free-for-all, no cheaters plz"

--server update
local data, ip, port = udp:receivefrom()
if data == "get data" then
    udp:sendTo("match named "..match_name, ip, port)
end
On the client, you want to maintain a list of confirmed matches:

Code: Select all

--client load
confirmed_matches = {}
local IPs = get_matches()
...

--client update
local data, ip = udp:recievefrom()
if strsub(data, 1, 12) == "match named " then
    local match_name = strsub(data, 13)
    table.insert(confirmed_matches, {ip=ip, name=match_name})
end
Now confirmed_matches will fill up with responses from the servers as they arrive, including the match names. In this example, the data is only the reply "match named " and then the rest of the data is the name. Obviously you'll need to figure out your wire format by your actual data requirements.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() »

Thnx for the details. So question is that everything is okay with the for loop and setpeername?(Plus do I need to send the IP since it's already known to the client through get_matches()?)
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii »

Gunroar:Cannon() wrote: Fri Jan 15, 2021 4:54 pm everything is okay with the for loop and setpeername?
Yes.
Gunroar:Cannon() wrote: Fri Jan 15, 2021 4:54 pm do I need to send the IP since it's already known to the client through get_matches()?
When receiving data from a socket, the sender's IP address is included automatically in receivefrom()
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() »

I checked it now and it said udp has no index reciecefrom() and recieve, does this happen?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests