HTML5 + Javascript: Network for the game

I noticed that I was tired of trying to make games with high-level programming languages ​​such as C # using OpenTK. C or C ++ frames look a bit outside of my humble self. I just got a sudden desire to go back to some web development and try to make a browser game - in pure HTML5 + JS, of course!

And although I think that sooner or later I can figure out the canvas using ze internetz, I just do not quite understand how I should work with the network.

WebSockets seem interesting, but are they the right way, seeing that they are still pretty undeveloped? AJAX sounds a bit slow and bulky. I do not plan to make a game that requires very low latency, but I want it to be low enough to play smoothly.

What would you suggest?

+6
source share
6 answers

I think you are looking for comet technology that is used to implement push server technology. You can find additional information at the following links:

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

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

+3
source

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

WebSockets will provide you with the lowest latency browser connection. WebSockets are actually quite universally available because their WebSockets Flash emulator is web-socket-js , which can be automatically loaded if no built-in WebSocket support is found. Using web-socket-js emulation will have a higher latency than native WebSockets, but still below AJAX / long-poll.

In terms of WebSockets availability, native WebSockets (version 03) are 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 supported on iOS since version 4.2. I work in the HyBi working group (WebSockets), and the next iteration of the protocol, which deals with security issues from Mozilla and Opera, is very close. Mozilla and Opera are actively working on the implementation, so I expect that at least the next major releases from them will include WebSockets by default. But even in this case, support for Flash backback and iOS, WebSockets is now available almost everywhere.

If you want to use a Javascript server too, I would recommend Socket.IO . This is the backend node.js plus the client JS library. By default, WebSockets, if the browser supports it, includes the web-socket-js Flash flash reserve and can use a long poll if the WebSockets connection does not work for any reason (or you decide to disable WebSockets as a transport).

+3
source

Perhaps you can read more about the various options . Google will provide you with research material.

Depending on the amount of data exchanged between the server / clients, socket.io seems good enough for up to 10 updates per second for up to 100 people. Of course, although even Javascript will bring a lot of overhead, people who create network code in C ++ still complain about connection problems and speed even when using a very specific approach (data packaging, caching tcp / ip packets, etc.) .

+2
source

If you want to stay with HTML5 only, WebSocket is a very interesting technology, especially if you need your data to move as fast as possible. This is the only technology that will allow you to transfer data in both directions with the server. If your game does not need to be updated as soon as they are available, Ajax is enough.

You also need to ask yourself which browser you want to support for your game. WebSocket is not supported by all browsers.

If you are open to other options, it is possible to make a P2P connection in a browser with RTMFP in Flash . This allows you to directly execute all the client-client, rather than transferring the server to retransmit data. It is in Flash, but allows you to hide this functional function so that you have all your application logic in Javascript. This technology allows you to transfer more data without overloading the server, but the big drawback is the support. Flash is not supported on the Unix platform and is a third-party plugin.

+2
source

I would suggest Ajax. The key to networking in online games with HTML5 and JavaScript is to minimize server traffic (as in any other web application), so all you have to do is find a way to do this. What type of game do you mean, exactly? Do you really need a permanent Internet connection?

However, Ajax is neither slow nor bulky. It is as fast as any other internet connection in any board game.

+1
source

for network code that you might want to try working with existing infrastructure, such as the union platform. here is a multi-user drawing application written in pure js / html5 without websites:

http://www.unionplatform.com/?page_id=2762

also worth a look at www.socket.io. socket.io provides a raw websocket shell with xhr fault tolerance but no higher level apis (e.g. no rooms, no users, no accounts, no data exchange / management for things like ratings, etc.).

Colin

+1
source

Source: https://habr.com/ru/post/885948/


All Articles