adding lan support in a HTML5 game using javascript - node.js

i want to add lan support to tic-tac-toe game which is a HTML5 canvas based, so that two players can easily play. how can i do it using javascript/node.js?

OK, I'm assuming all of your HTML5 game is finished and you're distributing it with the server that hosts it. (Otherwise you're stuck to just the browser, which can't do all the awesome node.js net stuff, I am describing.)
NB: You need to have node.js running on both clients. Otherwise you can't do LAN-only without a central server's involvement.
One of your clients has decided they want to host a LAN game
The node.js server running on that client starts to listen on a port
That server find out its local IP Get local IP address in node.js
Options:
Your players can manually tell each other the IP of the hosting server
Your hosting server sends out broadcast messages to its local subnet, informing them that it's hosting a game
Your other client server scans for IPs in its local subnet to find any hosting gameservers (Tries to open the pre-determined port your hosting server is listening on.)
The other node.js client opens a connection to the hosting server.
Done.

Related

Webserver hosted on an ESP32 using p2p

I was thinking about how could i host a webserver from an esp32 that is accessible from outside of it's network.
The first thing i did was i port forwarded my esp32 private ip on my router and configured a dns server to my router's public ip ( no-ip , ddns.net )
That was good. I could reach my webserver hosted on my esp32 but if i want to create a product, this is not a good solution because i can not force the users to configure their routers and configure a dns provider for themselfs.
So the next thing in my mind was, i needed a server. I could have used mqtt or any other service to exchange data with the client but i wanted to do it myself.
So i set up a node js server on my PC. The esp32 can connect to this server because it is on a public network and port forwarded already. The esp32 can use websockets as a client or http requests to send data to the server. This is ok, but the web app that the client sees is not hosted on the esp32, but on the server. This makes it hard to update the web app files ( html and js files ) both on the server and on the esp32. Because what if the client want to reach the web app from within it's local network without internet. The esp32 should have the exact same web app hosted like on the server.
I was using socket io to communicate the esp32's data with the web app client. The node js server served the static html and js files to the client, and forwarded the esp's messages coming on websocket throught socket io to the client.
Okay so the problem in here as i said that i had to update the files on both the node server and on the esp32. This is not a good way.
So i need some kind of a p2p connection. At this point i read about hole punching and webrtc and things like that.
What i tried to establish p2p connection:
Esp32 connects to node js server every 100ms, sending it's router's
public IP, it's own private Ip and things like that.
Server stores the esp data.
Client connects to server, fetching esp's router public ip, trying to
connect to it.
Server sends a message back to esp32 as a response with the client's
ip address.
Esp get's the message, trying to send data to client's IP address.
Client trying the same...
aaand nothing happens. The router's don't want to open their ports.
I tried this with raw udp, http requests and websockets. These were unsecured connections.
I found literally no low level explanation about web rtc's data channel. If i would want to write this "open source" protocoll using the C language there is just the explanation about this webrtc with high level languages like *built in options on browsers in JS and things like that.
ESP32 microcontroller LINK.
The question is, how could i estabilish a p2p connection between a microcontroller ( like esp32 using esp idf or arduino framework ) and a browser client? Is it even possible?
Have you considered mDNS/DNS-SD? You can advertise your service from esp32 and discover it with your client via arbitrary network

Does a Node js web server need a domain name to communicate with clients on other devices?

I am working on a swift project for osx with Firebase. I have a node web server to communicate between the clients and the Firebase-server, but it's a localhost-server. Do I need a real domain name to make the server accessible to end-users on another device? (I don't want a web app, just the backend for myself)
you doesn't need a domain .. but you need a serve to deploy having ip address .. suggestion you can use cloud server
You have two ways:
make request on port that the nodejs uses, example http://101.01.01.01:8000
use nginx like proxy, in this setup make your requests on 80 port (it's default), example http://101.01.01.01.
If you wont make something like dev environment on local machine use first case (don't forgot open port for other devices), for production - second.

Coordinating Client and Server side Ports (TCP/IP)

I recently started experimenting with TCP/IP in Java (question not Java specific), and I was wondering if there was a work-around to this problem: I want my Client to be able to list all available Local Network servers. This is easy when every server and client use the same port, eg. 48,000 - just cycle through every possible IP and ping any existing servers...
But what if a user already had a game open that was using port 48,000? The game would just crash with no work around. A fix would be for the server to loop and test every port between say 48,000 and 60,000, but then how would the client know which port to connect to?
I want to be able to list all available servers (like a game such as Halo does), without the user having to specify a port number.
Thanks for the help

What is the best way to test a video chat application locally?

If I am running a local server that is hosting a video chat application what is the best way to test the application with another user? Lets say I have two computers, is there a way I can allow computer B to connect to the local server that is running on computer A? The goal is to simply test whether the video chat application works properly in two completely different browsers running on separate computers. If there is a way I can trick the app into thinking the computer that is running the local server is another host, please let me know.
But Remember that the app needs access to the computers webcam. Thanks!
Addition Info: Nodejs application that uses the Tokbox, express, and socket.io APIs
Just make sure your web server is listening on a routable IP like "0.0.0.0", which you specify when you call http.createServer(port, ip, handler). Then point the browsers at the server's IP address such as http://192.168.1.1:3000 (fill in your specific IP and port). You can use the command ifconfig -a on linux or osx to get your server's local IP address, and ipconfig on windows.

Socket.io on port 80

I've made a node application which listens on port 80, my application works fine on localhost, but when I run it on my VPS, I get a different log and a different result ( websockets just don't work )
A comparison between localhost's log and VPS' log:
Node's log on localhost
Node's log on VPS
As you see, in VPS, xhr is used instead of websocket after it says "info: transport end (socket end)"
I don't use any web server on my VPS and I ran my application as root.
Are you running a web server in front of your node app on your VPS? If so, make sure it is new enough and properly configured to do websockets. For instance, on modern Ubuntu the stock nginx is not new enough yet to support web sockets, so you have to install a separate package to get websocket support.
2nd guess: is there a proxy server between your browser and your VPS?
Have you run it as the super user on the VPS? Normal users are typically blocked from opening ports below 1024.
Our server hosted on VPS, using port 80. The io connection fired through cellular data and through WIFI fine, but in some wifi networks it didn't.
So we had used different port, then it works.

Resources