1

I'm making a web based game with my friends in which, two people are on 2 different computers and are playing on chrome or firefox. How can I transfer the game data between the 2 computers if they are on the same wifi network? Are there any APIs for this? Does HTML5 have anything for this?

P.S. I don't have a server

user2420649
  • 223
  • 1
  • 3
  • 9
  • 1
    Does it need to be real-time? – Marty Jun 19 '13 at 03:33
  • 2
    Yes. Assuming only Firefox and Chrome are involved you can use WebRTC for real time peer-to-peer communication between computers. It's very new and experimental at the moment. Chances are you'd still need a server just to 'introduce' them to each other. [Here is a tutorial to get you started](http://www.html5rocks.com/en/tutorials/webrtc/basics/) – Benjamin Gruenbaum Jun 19 '13 at 03:35
  • 2
    @BenjaminGruenbaum: Very interesting. I was not aware of this. Thank you for teaching me something today! – Sébastien Renauld Jun 19 '13 at 03:38
  • http://blog.printf.net/articles/2013/05/17/webrtc-without-a-signaling-server/ – Benjamin Gruenbaum Jun 19 '13 at 03:57

2 Answers2

1

You can use the data-channel of WebRTC:
http://www.webrtc.org/

or you can set up a server and use WebSockets:
https://en.wikipedia.org/wiki/WebSocket

Drawbacks:

For WebRTC you will need a STUN server as I understood it, that will exchange the IP-addresses you use. For the rest the comminication is peer-to-peer.

It's currently scarce with examples on WebRTC as it is still in development. Only Chrome and Firefox (nightly) supports the protocol.

html5rocks has written a nice introduction on this (too much to paste in here):
http://www.html5rocks.com/en/tutorials/webrtc/basics/

For WebSockets you need a web server that function as a hub. This is however simple to set up as it uses the standard HTTP protocol. If you're on the .Net platform you can use for example SignalR which is easy to get up and running.

Note that for WebSocket it must initially be same-origin. This is however possible to circumvent (CORS). Typically the script using WebSockets is within the same web page and will communicate also with the same domain it was loaded from.

There are other options for WebSockets that use can use with node.js, socket.io, ruby and so forth.

UPDATE:

If you don't have a server peer-to-peer is gonna be difficult with plain html5. You will have to have something that negotiates the connections.

You can work around this by setting up a server locally on your computer and allow the other person to connect to that directly.

See for example this (free open-source TURN/STUN server):
http://code.google.com/p/rfc5766-turn-server/

Note: this server targets *NIX. I couldn't find a free solution for Windows if that's what you're using. There are commercial solutions as another option.

  • Worth mentioning, if you use web sockets _all_ your data will go through the server. Using WebRTC only the introduction is done through the server but all actual game communication is P2P. You can do both options using nothing but JavaScript if you'd like. WebSockets with socket.io and nodejs for example. – Benjamin Gruenbaum Jun 19 '13 at 03:42
  • @BenjaminGruenbaum usually true, you can setup server/websockets for not same origin, but typically this isn't a problem since the script is usually run in the same page that was loaded. –  Jun 19 '13 at 03:43
  • Cool :) How would some WebRTC code for accomplishing the very most basic version of what OP wants look like? – Benjamin Gruenbaum Jun 19 '13 at 03:46
  • @BenjaminGruenbaum Hmm, there are scarce amount with examples for WebRTC at this point. But a look at html4rocks' introduction will give a briefing/intro and make it easier to determine if one wants to dive into this uncharted territory. :) –  Jun 19 '13 at 03:52
-1

There are a few ways of tackling your problem, and which method works best will depend on your game requirements and how you are building it.

I am not aware of any technology that will directly send from browser to browser without some sort of server sitting in between.

Have a look at either HTML5 Websockets: http://www.tutorialspoint.com/html5/html5_websocket.htm

Alternatively you can store the data for your game and have the game client retrieve the necessary information from the database upon each turn or tick timer etc.

JanR
  • 6,052
  • 3
  • 23
  • 30
  • WebSockets do not magically traverse the same-origin security restrictions. – Sébastien Renauld Jun 19 '13 at 03:38
  • 1
    Your answer is off-topic as the user wanted a peer-to-peer distributed game, whereas you are offering a solution for a server-centric game. Even if this was not the case with WebSockets, you would need to provide a way to turn the sockets into the same origin, which is impossible. RTCPeerSockets are the way forward on this one. – Sébastien Renauld Jun 19 '13 at 03:43
  • I don't think it's off-topic. The user wants to transfer game data between 2 browser instances. I have provided a solution for that, peer to peer was not mentioned anywhere. I do agree that RTCPeerSockets seems to be way to go with peer-to-peer connections. – JanR Jun 19 '13 at 03:46
  • 1
    @JanR The title of this question is "Peer to Peer web API on the same network" – Benjamin Gruenbaum Jun 19 '13 at 03:52
  • Yeah saw that after I posted... Lack of coffee much? :) – JanR Jun 19 '13 at 03:53
  • @JanR: question title. Obvious enough. – Sébastien Renauld Jun 19 '13 at 03:53