spynaz wrote:Wait do you mind explaining your code a bit? Because I'm kind of confused.
Haha, that's the bad thing about game jams: no time to comment your code.
I didn't write the UDP server, a friend did - but I'll try my best:
Server side:
The server.lua is started, using plain lua. We chose not to use love for this, because for the plain server, all you need is lua socket (see below). This is just a design choice. If you want your server to also be a client, you can do that by simply calling all the stuff I explain below using love, not plain lua.
In linux, do:
On windows, you'll need to install Lua 5.1 and then run the script like this (I guess):
Code: Select all
cd path\to\KarmaExchange
C:\Program/ Files\Lua\Lua.exe server.lua
Or similar.
The server.lua script starts by running the serverSetup() function (see last line in server.lua).
At the beginning of the file,
lua socket is required (you might need to install that) and then two udp sockets are created. The socket called
udp is just relevant for our game, you will probably not need it. The
broadcast socket is what you should look at. You should probably read the introduciton to udp here:
http://w3.impa.br/~diego/software/luaso ... ction.html
At the beginning of serverSetup() the udp sockets are bound to the addresses "*" (representing all addresses) and two ports (dfined in common.lua)
Make sure to also add the two lines:
Code: Select all
assert(broadcast:settimeout(0))
assert(broadcast:setoption('broadcast', true))
The first one makes sure that any following calls to send and receive functions on the broadcast socket won't lock the game; if there's no data on the sockets, the call will return and your game can continue.
There's lots of stuff related to our game following which you won't need. What's important is the lines
Code: Select all
local b_pack, b_ip, b_port = broadcast:receivefrom()
and
Code: Select all
local success, e_msg = broadcast:sendto(B_SERVER_MESSAGE, B_ADDRESS, B_PORT)
receivefrom needs to be called in a loop, to always accept incoming requests. So if a client broadcasts something on the port that broadcast was bound to, then this call will receive it. b_ip and b_port will return the address and port of the client so we can send back data. That's what we do using the sendto function. So as soon as we get a request, we send back a message. By receiving this returned message, the client will know that there's a server.
Client side
The client is written using the normal Love API: main.lua ist started as always. The client script client.lua is required.
In client.lua, the procedure is similar: a udp and broadcast socket is created. Again,
broadcast is the important one.
First, setupBroadcast() is called, which set the same ports and address as the server did. It also sets the socket as a broadcast socket and the timeout to 0.
Then sendBroadcast() is called, which sends any message on the broadcast socket and then returns immediately. After this, in love.update, receiveBroadcast() is called, which continuesly looks for answers. If there's an answer received, it adds the ip and port from which it received from the server list.
So:
1) Client sends request to socket and starts listening for answers
2) Server finds it, replies: "I'm a server"
3) Client receives the reply and notes down from which address it came. Now the client can display that data in a list and the player chooses one. Then your game can connect (using tcp, for example) to that server.
In our script, the messages sent are simply:
B_CLIENT_MESSAGE = "Who's a server?"
B_SERVER_MESSAGE = "I'm a server
"
But of course, the server could instead send back a server name, description, how many players are online, what map he's hosting etc... This was just not needed for our game.
I hope this helps somewhat.
T-Bone wrote:If you have an old computer you can just install Linux on it and use it as your webserver. No-ip provides free URLs for it. It would have to be on 24/7 but they don't consume that much power (I've measured, my Minecraft server uses less power than a light bulb).
I use a raspbery pi (even lower power consumption) for my server atm. If you choose this method, I really recommend writing all the scripts in plain lua (or html/php/mysql), because running love on a raspberry pi works but is a hazzle. I also use no-ip.org, it works well with a pi.
But you'll need to remember that these are two different things entirely: One option is the udp broadcast, which works inside a LAN and does not require port forwarding etc. The other one is an online server list which requires each player who hosts a server to open his own ports for the game if you want players from the internet to access his/her server.