1

I've created a game using html5 and websockets that is played with both a pc and a mobile device (multiple mobile devices can also be played):

The game has a Stage - The PC or a tablet device.

And several players — Other mobile devices

The current connection between the Stage and Players is with a number (the Stage shows a number and a player can join through that number), however, I'm searching for a way that the Players can join just by being in the same local network as the Stage - meaning auto-discovery through a local network.

I've been searching WebRTC and other p2p solutions but I can't find anything. Is there something else I can use or is it completely impossible?

Eliezer Broide
  • 95
  • 1
  • 1
  • 7
  • possible duplicate of [Phonegap Bonjour/Zeroconf or Websocket IP Discovery from HTML5](http://stackoverflow.com/questions/8676590/phonegap-bonjour-zeroconf-or-websocket-ip-discovery-from-html5) – ceejayoz Jan 25 '15 at 16:24

2 Answers2

4

You can try this cordova-plugin-networkinterface plugin that supports Android, Blackberry 10, iOS and Windows devices.

Luis U.
  • 2,500
  • 2
  • 17
  • 15
2

There's no javascript only way to the local ip address from inside a browser. There do seem to be a good number of cordova/phonegap plugins that purport to do this:

http://plugreg.com/plugin/weconstudio-it/phonegap-plugin-ipaddress https://github.com/jcesarmobile/my-phonegap-plugins/tree/master/iOS/NetworkInterfacesPlugin

As well as the one pointed out in the comment above. It looks like it's either android or IOS. Someone should write one that works on both!

If it was me. I would probably work with the browser headers on the Stage: REMOTE_ADDR, HTTP_CLIENT_IP, etc. There are some fine points to doing this: http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html

On a local network, you'll be able to get the real IP address of each device. Private networks fall into a particular address space:

10.0.0.0        -   10.255.255.255
172.16.0.0      -   172.31.255.255
192.168.0.0     -   192.168.255.255

So your Stage will know when its clients are connecting to it on a LAN. Does that get you where you need to go?

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • Well I can take your idea and use it on just the websockets to tell me the IP address (I'm using socket.io so http://stackoverflow.com/questions/6458083/socket-io-get-clients-ip-address). However, will that be enough to ensure that the Stage and Players are at the same local network? And for that matter, is there a way for them both to send messages directly to each other? – Eliezer Broide Jan 25 '15 at 18:42
  • It seems to me that if the IP addresses are in the ranges above, everyone must be on the same LAN. – Robert Moskal Jan 25 '15 at 18:56
  • Of course, I'm just asking if it would be a way that I would get the IP address every time (in case of proxy or whatever) – Eliezer Broide Jan 25 '15 at 18:58
  • Good question. Is the Stage also running as a phonegap app? Can it listen on multiple IP addresses? Can it be moved on and off a network without restarting the server? You'll have to think through all these cases and more. Seems to me you're going to want to check the IP address of a caller every time. – Robert Moskal Jan 25 '15 at 19:33
  • Stage has a possibility of running as a phonegap application (The app would be either able to join as a new player or start a new game - i.e creating a new Stage). In the meantime I didn't create any type of P2P or server out of it because I have no idea if its possible. So for now I'm just using websockets and a remote server to connect between devices. Stage is not likely to move to another network, however, it will still be connected to the same websocket, allowing to continue playing (in case it didn't disconnect by moving to another network) – Eliezer Broide Jan 26 '15 at 02:26
  • Also, almost forgot, if there is some way of doing it P2P over LAN it will be better for me, even if that means that the Stage would not be able to be hosted on the browser (but only on Devices via Cordova or an installed application on pc/mac) – Eliezer Broide Jan 26 '15 at 02:34