Home

Awesome

Browser extensions for netcode.io

This repository enables the use of netcode.io via browser extensions, prior to its adoption in browsers. netcode.io enables the secure use of UDP in web browsers for the purpose of supporting multiplayer HTML5 games. More information on netcode.io can be found here.

gif of browser support

Supported Browsers

netcode.io is supported in the following browsers via extensions:

Mobile device support is basically impossible until netcode.io support appears in browsers natively, as mobile platforms don't support extensions or native messaging.

Installation

From Source

For this to work, it requires the installation of both a browser extension and a native application helper which performs the actual netcode.io communication. The extension uses the native messaging APIs provided in browsers in order to make netcode.io available via the helper.

To try this out in your browser, first build the netcode.io.host helper:

1. Get dependencies

go get github.com/wirepair/netcode

2. Platform specific build instructions

3. Install (run this from the command-line)

./netcode.io.host

This should run with no errors, and install itself as a native messaging extension.

After this is done, add the browser\webext directory as an unpacked extension.

netcode.io API

The API made available to Javascript clients as window.netcode. You can check for the availability of netcode.io using the conventional if (window.netcode) { ... } pattern.

window.netcode provides one function: createClient(callback):

window.netcode.createClient(protocol, callback)

Creates a new netcode.io client which can be used for secure UDP communication using the netcode.io protocol. The callback is of the form callback(err, client). If err is set, there was an error creating the client (and client will be null). Otherwise client is set and err is null. The returned client is an instance of Client.

protocol should be either ipv4 or ipv6 and determines what type of server address you can connect to.

Parameters:

window.netcode.isNativeHelperInstalled(callback)

Checks to see if the netcode.io helper has been installed by the user. The callback is of the form callback(err, isPresent), where err is either null or an instance of Error. If there is no error, isPresent is a boolean indicating whether the native helper has been installed.

If the netcode.io helper has not been installed, you should direct the user to https://github.com/RedpointGames/netcode.io-browser/releases/tag/latest where the netcode.io helper can be easily installed in a few clicks.

Parameters:

Client.setTickRate(tickRate, callback)

Sets the tick rate of the netcode.io client, expressed as the number of ticks per second for receiving and sending packets. The tick rate for clients defaults to 60; that is, 60 times a second.

Parameters:

Client.connect(token, callback)

Connects to a netcode.io server using the specified token. The token should be an instance of Uint8Array and represent a netcode.io token received from an authentication server.

You can not use netcode.io to send UDP packets to arbitrary IP addresses; instead, you must have an authentication server that uses the netcode.io library (or a compatible implementation) which can generate and sign tokens with a list of game server IP addresses and a private key shared between your authentication server and game servers.

In most common scenarios, your authentication server will provide the token as part of an AJAX request. If you are using a user account system, you'll provide tokens after the user logins into your game, with the token indicating which server the client is authorized to connect to.

Parameters:

Client.send(packetBuffer, callback)

Sends a packet to the connected server with packetBuffer as the data. packetBuffer should be an instance of Uint8Array.

Parameters:

Client.getClientState(callback)

Returns the current state of the client as a string. The returned state is one of: connected, connectionDenied, connectionRequestTimeout, connectionResponseTimeout, connectionTimedOut, connectTokenExpired, disconnected, invalidConnectToken, sendingConnectionRequest, sendingConnectionResponse or destroyed.

Parameters:

Client.destroy(callback)

Destroys the client, disconnecting it from the server and cleaning up any associated resources. Once a client is destroyed, it can't be reused.

Parameters:

Client.addEventListener(type, callback)

Adds an event listener to the client. Currently the only supported type is receive, which is fired when the client receives a packet from the server.

For receive the callback is of the form callback(clientId, buffer) where clientId is the client identifier issued originally by the authentication server and buffer is the received packet as an instance of Uint8Array.

Parameters:

License

This host extension code is provided under the MIT license.

Contributing

The primary goals for this project currently are:

All pull requests must be made available under an MIT license.