Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 + Javascript: Networking for a game [closed]

I noticed I'm getting tired of trying to make games with high level programming languages like C#, using OpenTK. C or C++ stills looks a little out of range for my humble self. I just got the sudden urge to get back to some web development and try to make a browser game - in pure HTML5+JS of course!

And while I think I can figure out the canvas myself sooner or later, with the help of ze internetz, I just don't quite know how I should handle networking.

WebSockets seem interesting, but are they the right way to go, seeing as they're still pretty undeveloped? AJAX sounds a little slow and bulky. I don't plan to make a game that requires very low latency, but I do want to keep it low enough to play smoothly.

What would you suggest?

like image 754
copyboy Avatar asked Apr 15 '11 02:04

copyboy


4 Answers

I think what you are looking for is the comet technology which is used to implement server push technology. You can find more information from following links:

http://en.wikipedia.org/wiki/Comet_(programming)

http://infrequently.org/2006/03/comet-low-latency-data-for-the-browser/

like image 126
xiao 啸 Avatar answered Nov 11 '22 08:11

xiao 啸


If latency is not a big issue then you should probably just use one of the many good AJAX/long-poll libraries.

WebSockets will get you the lowest latency browser communication. WebSockets is actually fairly universally available because their is a WebSockets Flash emulator web-socket-js that can be automatically loaded if native WebSocket support is not found. Using web-socket-js emulation will have higher latency than native WebSockets but still lower than AJAX/long-poll.

In terms of WebSockets availability, native WebSockets (version 03) is supported by Chrome and Safari. Version 03 is also supported by Firefox 4.0 and Opera 11 but is disabled by default. WebSockets is also support in iOS natively since 4.2. I'm in the HyBi (WebSockets) working group and the next iteration of the protocol that addresses the security concerns from Mozilla and Opera is getting very close. Mozilla and Opera are actively working on implementations so I expect that at the latest the next major releases from them will have WebSockets turned on by default. But even so, the the Flash fallback and iOS support, WebSockets is available pretty much everywhere today.

If you are willing to do Javascript server-side too, then I would recommend Socket.IO. It is a node.js backend plus a client side JS library. It defaults to WebSockets if the browser supports it, includes the web-socket-js Flash fallback, and can use long-poll if the WebSockets connection doesn't work for some reason (or you choose to disable WebSockets as a transport).

like image 39
kanaka Avatar answered Nov 11 '22 06:11

kanaka


Perhaps you might read up more on different options. Google will provide you with research material.

Depending on the amount of data exchanged between server/clients, socket.io seems good enough for up to 10 updates/second for up to 100 people. Mind though that even Javascript will introduce alot of overhead, people that create networking code in C++ still complain about connectivity and speed issues even when using highly specific approach (data packing, tcp/ip packet caching etc).

like image 23
Daniel Protopopov Avatar answered Nov 11 '22 07:11

Daniel Protopopov


If you want to stay with only HTML5, WebSocket is a very interesting technology especially if you need your data to move as fast as possible. It's the only technology that will allow you to stream data in both direction with the server. If your game doesn't need to have update as soon as their are available Ajax is enough.

Also you need to ask yourself what browser you want to support for your game. WebSocket is not supported by all browser.

If you are open to other options, it is possible to do P2P connection in the browser with RTMFP in Flash. This allows you to do all the "client to client" communication directly instead of passing by a server to retransmit the data. It is in Flash, but it's possible to bridge that functionnality so that you have all your application logic in Javascript. This technology allows to pass more data around without overloading your server, but the big downside is the support. Flash is not well supported on Unix platform and it's a 3rd party plugin.

like image 2
HoLyVieR Avatar answered Nov 11 '22 07:11

HoLyVieR