Home

Awesome

<!-- [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=MirrorNetworking_SimpleWebTransport&metric=coverage)](https://sonarcloud.io/dashboard?id=MirrorNetworking_SimpleWebTransport) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=MirrorNetworking_SimpleWebTransport&metric=alert_status)](https://sonarcloud.io/dashboard?id=MirrorNetworking_SimpleWebTransport) -->

Discord release openupm

Simple Web Transport

Low level Websocket Transport use by Mirror and Mirage

This Transport uses the websocket protocol. This allows this transport to be used in WebGL builds of unity.

Includes a Websocket server, standalone client, and a WebGL client so that both your Server and Client can be build with Unity.

Download

UnityPackage

  1. Download the code from the source folder or package on Release page.
  2. Put the code somewhere in your Assets folder

openUPM

  1. Add openupm registry. Click on the menu Edit -> Project settings..., and add a scoped registry like so: <br/> Name: OpenUPM <br/> Url: https://package.openupm.com <br/> Scopes:
    • com.james-frowen
  2. Close the project settings
  3. Open the package manager.
  4. Click on menu Window -> Package Manager and select "Packages: My Registries",
  5. select the latest version of SimpleWebTransport and click install, like so:
  6. You may come back to the package manager to unistall SimpleWebTransport or upgrade it.

Or add to Packages/manifest.json

{
    "dependencies": {
        "com.james-frowen.simplewebtransport": "2.2.0"
    },
    "scopedRegistries": [
        {
            "name": "package.openupm.com",
            "url": "https://package.openupm.com",
            "scopes": [
                "com.james-frowen.simplewebtransport"
            ]
        }
    ]
}

Usage

Below are some examples of how to set up a server and client so that they will connect and low any message sent between them

Server

// create server instance
var tcpConfig = new TcpConfig(noDelay: false, sendTimeout: 5000, receiveTimeout: 20000);
var server = new SimpleWebServer(5000, tcpConfig, ushort.MaxValue, 5000, new SslConfig());

// listen for events
server.onConnect += (id) => Debug.Log($"New Client connected, id:{id}");
server.onDisconnect += (id) => Debug.Log($"Client disconnected, id:{id}");
server.onData += (id, data) => Debug.Log($"Data from Client, id:{id}, {BitConverter.ToString(data.Array, data.Offset, data.Count)})");
server.onError += (id, exception) => Debug.Log($"Error because of Client, id:{id}, Error:{exception}");

// start server listening on port 7777
server.Start(7777);

// call Process to cause events to be invoked
// Call this from inside Unity Update method so that it will process message each frame
server.ProcessMessageQueue();

Client

// create client instance
// call static SimpleWebClient.Create method so that the correct client for WebGL or standalone is created
var tcpConfig = new TcpConfig(noDelay: false, sendTimeout: 5000, receiveTimeout: 20000);
var client = SimpleWebClient.Create(ushort.MaxValue, 5000, tcpConfig);

// listen for events
client.onConnect += () => Debug.Log($"Connected to Server");
client.onDisconnect += () => Debug.Log($"Disconnected from Server");
client.onData += (data) => Debug.Log($"Data from Server, {BitConverter.ToString(data.Array, data.Offset, data.Count)})");
client.onError += (exception) => Debug.Log($"Error because of Server, Error:{exception}");

// create url and connect to server
var builder = new UriBuilder
{
    Scheme = "ws",
    Host = "www.example.com",
    Port = 7777
};

client.Connect(builder.Uri);

// call Process to cause events to be invoked
// Call this from inside Unity Update method so that it will process message each frame
client.ProcessMessageQueue();

Send message to Server

Once the client is connected (after the onConnect event or check ConnectionState.) message can be sent

byte[] message = Encoding.ASCII.GetBytes("Hello Server");
client.Send(new ArraySegment<byte>(message));

Most of the time you will want to create a message Id so that the server can know what the message should be. But for this example we just send a string, if using the server example above it will log the bytes from the message when it is received

Bugs?

Please report any bugs or issues Here

Websocket Secure

This transport supports the wss protocol which is required for https pages.

Reverse proxy

Using a reverse proxy for SSL is generally more efficient than implementing SSL directly within Unity or other game servers, as the proxy can specialize in handling encryption, freeing up the game server to focus on delivering smooth gameplay.

To use reverse proxy with Mirror see this page Mirror/README.md

Nginx

Nginx is a popular reverse proxy thaat is easy to set up, and also has the ability to host WebGL files making it easy to set up development server. For a short guide on Nginx see this page: NginxConfig

Other reverse proxies

The Mirror docs explains how to set up some othher revierse proxies,

(not recommended) How to create and setup an SSL Cert

If you host your webgl build on a https domain you will need to use wss which will require a ssl cert.

See this page

Logging and Debugging

Logging is disabled by default to increase performance.

Log levels can be set by using Log.level = Log.Levels.warn.

Log methods

Log methods in this transport use the ConditionalAttribute so they are removed depending on the preprocessor defines.

These preprocessor defines effect the logging

Without SIMPLEWEB_LOG_ENABLED info or verbose logging will never happen even if log level allows it.

See the Unity docs on how set custom defines.