Get matching LAN players - javascript

I'm running a simple node.js server that also gots a webinterface using express.
Every time a clients goes on the website I'd like to find out who else from this clients network is connected.
I know that I can read out the localIP address of every client using some syntax like this.
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null);
... but how can I determine which clients are in the same network? (LAN)
I'm aware that the IP-address is nearly unnecessary/needless because of the currently unkwown Network ID - but how can I determine the networkID on the nodeJS server?
Edit: Is this also possible using socket-io with node.js?
Any help would be really appreciated. Thanks.

If you just want to check if two players are in the same LAN, looking at their remoteAddress using your code should be enough. You might want to assign unique IDs to each player, so if you have 2 players with different IDs but same IP address, it means that they are in the same LAN.
On the other hand, if you really want to obtain their local IP address for other purposes, you might want to look at this answer on how to process os.networkInterfaces()

Related

Socket in javascript

please what is different
var socket = new WebSocket('ws://localhost:8181');
var socket = new WebSocket('ws://localhost:8181/websession');
what is ( Websession )
websession is just the endpoint the websocket will connect to. It's just like normal HTTP servers or REST services: You can have multiple endpoints on one server, like:
ws://localhost:8181/customers
ws://localhost:8181/prices
ws://localhost:8181/items
... and so on. (This is just an example and does not necessarily make sense for a specific use case.) In old-style HTTP, you could image them as different directories on the same server, possibly offering very different contents.
In order to use the socket correctly, you have to know your desired endpoint and use it when creating the socket. So it depends on the server whether ws://localhost:8181 or ws://localhost:8181/websession is correct (or even both of them, depending on the purpose of the individual endpoint). It's generally a good practice to give the endpoint a meaningful name, so the first one would be discouraged.
As the application seems to be running on your localhost, you should take a look at the server running at port 8181 to find out the endpoints offered. And you could possibly get used to websockets, here is one of many possible starting points.

WebRTC on shared hosting ( SSH ) without nodejs, preferrably in php

So ive been looking for a way to integrate webRTC into a site im making, but i want to do it on shared hosting. I came across this repo on GitHub by nielsbaloe and it has been a huge help in getting a basic connection.
This is the code i believe is responsible for adding the peer: ( index.html in the repo, line )
function icecandidate(localStream) {
pc = new RTCPeerConnection(configuration);
pc.onicecandidate = function (event) {
if (event.candidate) {
publish('client-candidate', event.candidate);
}
};
try {
pc.addStream(localStream);
}catch(e){
var tracks = localStream.getTracks();
for(var i=0;i<tracks.length;i++){
pc.addTrack(tracks[i], localStream);
}
}
pc.ontrack = function (e) {
document.getElementById('remoteVideo').style.display="block";
document.getElementById('localVideo').style.display="none";
remoteVideo.srcObject = e.streams[0];
};
}
Now the struggle im facing is adding room functionality, and maybe the ability to have more than two concurrent peers present at the same time. I did some experimenting, but in vain. I know that for room functionality id have to tinker around in the php, so at least id like to figure out how to make more than 1 peer possible.
As far as I know, there is no way to re-use the same RTCPeerConnection for multiple peers, so you'll have to do the same thing as 1-on-1 but between every single peerĀ in a group.
As far as signalling, it's pretty simple, goes kind of like this:
Client A -> [Offer] -> Server -> [Offer] -> Client B -> [Answer] -> Server -> [Answer] -> Client A
A nicer explanation at MDN
There isn't necessarily a need for NodeJS or WebSocket. The reason most people go for it it is because the last link in this chain (Server -> Client A) requires server-initiated connection. But that can be substituted with alternative techniques such as (long-)polling. Or, in case of PHP, you might use websocket implementations such as Bloatless or Aerys
To implement the room functionality, you'll have to implement the following:
Variant A (using polling):
An endpoint to throw offers at, e.g. POST /rooms/{id}
An endpoint to regularly check for new offers from, e.g. GET /rooms/{id}
Variant B (with websockets)
Create a broadcast rooms, for example, by dynamically creating HTTP endpoints and websocket server instances. Or by having a single websocket instance but sending whatever room you're intending to join right inside after establishing a connection. From there, it's only a matter of sending correct offers and answers to correct users.
In both cases, you might want to either create multiple offers in advance to pool from the server, or to dynamically create new ones, but, most importantly, make sure you're not connecting the same peers twice, otherwise you will end up with a loop. To prevent it, just provide each user with a randomly generated string to identify themself and send it among offers.
There are turnkey solutions available if you don't want to go this route, but be careful and check whether you can use your own TURN servers with them. A common trend I have noticed is that there are a lot of WebRTC solution providers out there that lure you with their simplicity but then lock you in with their own TURN servers for which you might have to pay a quite hefty bill later on.

Websokets(ws) , NodeJs Clusters,

I am building real time connection server. And want to scale it with Node Clusters
I went through many different websites trying to figure out how to implement Websokets(ws) and Node clustering. So the basic idea which i wanted to try is : I have and array of all users who are connected to the server , like :
{key:ws, key:ws ,key:ws}
So what i want to do is to run Node js Clusters with Websockets , i will get some thing like that
Master : place to store all the connected ws in array : {key:ws, key:ws ,key:ws}
Worker1 : Ws server to connect
Worker2 : Ws server to connect
What i am wondering is for example user which is connected to the Worker2 wants to send message to user which is connected to the Worker2 and worker1.
So i will send message from Worker2 to the Master to get all the users , Master will send {key:ws(worker1), key:ws(worker2) ,key:ws} . And Worker2 will iterate over this array and send message to each user.
Will that implementation work or message will not be send to the user which is connected to another Worker?
Thank you very much for the answer.
There is a package wse-cc that does fully-controlled user balancing between nodes, but the problem - it's not documented very well.
It uses different processes instead of clusters to make it able to work on multiple physical machines. I'm using it for browser MMO games. If you interested - I can make a quick doc on how to set up it. Not required any balancers on top, can work on a single machine or on multiple.
Or you can visit issues page - I'll help with any questions.

Check if client has already voted

I am making a poll site. And for making sure that people can't vote twice I'm saving their IP with the ID for the poll in a database. But this requires me to send the IP from the client to the server to retrieve the data out of the database. I am doing this all on the button: Vote so it checks when you click vote.
Now I am using Express and socketio and Node.js. I can't really figure out how to retrieve the IP.
The user does not have to register to vote or create a poll.
The method above might not be the best one and I think there might be a better one. So my question is:
What would be the best way to check if someone has already voted on a poll.
If you want to find the IP address, inside your express application:
app.get('/ip', function (req, res) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
//do whatever you want with it
});
And to answer your question, personally I'd require the user to type some sort of unique identification, like a university id number, tax number (thats how we're identified here in Brazil for example), anything that you can guarantee is unique to only one individual in your area of interest.
If you can't get something like that, I guess you'd have to register your users.

Trying to setup a node.js web server

I am new to web servers and node.js and I need some help.
I have no idea what to put in the .listen();
I think since I want it to connect to the internet the server needs to listen to port 80 but but I don't know what to put as the second value.
.listen(80, "What do I add here?");
Also i have a free domain name (www.example.co.cc) that is pointing to a dynamic dns (DnsExit) since I dynamic ip. I installed to program needed to update my ip address.
Is there anything I am missing?
Have you seen the example on the homepage of the Node.js project?
http://nodejs.org/
It clearly demonstrated .listen( 1337, "127.0.0.1" ); and then the next line reads Server running at http://127.0.0.1:1337/ - so the second argument is the IP you want to listen on. If you then take a look at the documentation you will see that this second argument is actually optional, if you omit it, Node.js will accept incoming connections directed at any IPv4 address.
http://nodejs.org/docs/v0.5.6/api/http.html#server.listen

Categories

Resources