📜 ⬆️ ⬇️

Browser-free multiplayer


About technology WebRTC recently written a lot. It allows for the interaction of two clients without a specialized server between them, but the need for a server is no longer necessary after the connection is established. And during the installation of the connection itself, some server resources are still needed. Can I get rid of the need for a server? A small experiment in this direction under the cut.

The need for a server for an rtc connection arises for two purposes. The first is the signaling server that provides the exchange of technical messages necessary to support the protocol itself. Fortunately, there are several ready-made implementations of the signaling server and they are already open on the network to everyone's pleasure. For example, the convenient peer.js library also offers a cloud signaling server.
Accordingly, this part of the question can not worry too much. Of course, the cloud server contains a bunch of restrictions on the number of connections, but for starters, this is quite enough.

But there is a second part. Before you start working with the signaling server for connecting via the rtc protocol, participants must exchange identifiers. The ID will allow the signal server to select the right participant from among the many potential recipients. And the delivery of information about the identifier lies on the shoulders of the developer. For example, peer.js tells us "I'm in charge of communicating the peer IDs between users of your site." I did not find a complete solution to this problem.

But what if we want to connect with a random subscriber? For an online game, this may be just what you need. A typical solution for this case is the creation of a special server that will store the database of registered identifiers. This is not too difficult, but I want to get rid of this server. After all, no one bothers to ensure the transfer of this identifier by means of an rtc connection opened with a fixed identifier. The idea is:
')

All code fits in fifty lines. After being issued as a mini-library (you can download it here: github.com/bay73/puzzleduel/blob/master/randompeer.js ) you can use it like this:

 var peer = new Peer({key: key}); connectRandom(peer, function(connection) { //      connection.on('data'), function(data){ /*   */}); connection.send(data); /*   */ }, function() { //      } ); 


You can view the full code and provide load testing of the toy made on its basis on github: bay73.imtqy.com/puzzleduel . Maybe someone will brighten up waiting for the end of the week. The main problem is that not all browsers (and not all versions) fully support WebRTC.

But to those who do not want to play the question - will this approach work with a noticeable user load?

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


All Articles