Keeping the noise down
These little ESP8266 NodeMCU modules are great little things.
You can get them to boot up, connect to a router and then immediately start broadcasting their presence over UDP. What's the point in that?
Well, the idea is that we would have our app listening on a UDP port, and when an incoming message is received from a new device just booting up, the server could "shout back" at it.
When the NodeMCU client hears the server shouting back at it, the client will have the server's IP address. At this point, the client could STFU and try to make a TCP connection to the server (to ensure connection stability).
If any of the UDP packets don't get through, there's no problem.
The client keeps shouting until it's heard back from the server. If the server misses the client's UDP packet, nothing happens - the client shouts again a few seconds later. If the server hears the client and shouts back, but that packet gets lost, nothing happens; the client just shouts again in a few seconds.
But when the client shouts and the server responds - and the client gets the response - as part of that response, client will now have the server's IP address and will establish a (quieter) TCP/IP connection to the server. Then everything settles down and nobody is shouting any more.
Sure, we could probably just have the server continually broadcasting its presence across the UDP network, and any clients that boot up could just automatically go into listening mode. But it feels like we're making the network unnecessarily "noisy" - since once a client has connected, neither client nor server are using UDP to talk to each other; since clients connecting would only take place right at the very start of a gaming session, it seems unnecessary to have the server shouting it's presence across the network when 99% of the time, nobody will be listening!

Comments
Post a Comment