M
M
Mirror
Search…
WebSockets Transport
Transport that uses the websocket protocol. This allows this transport to be used in WebGL builds of unity.
Simple Web Transport Inspector

Logging

Log levels can be set using the dropdown on the transport or or setting Mirror.SimpleWeb.Log.level.
The transport applies the dropdown value in its Awake and OnValidate methods.

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
  • DEBUG allows warn/error logs
  • SIMPLEWEB_LOG_ENABLED allows all logs
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.

Setting Up SSL

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

pre-setup

note: You may need to open port 80 for certbot

Get Cert

Follows guides here:
Find the instructions for your server version, below is link for Ubuntu 18.04 LTS (bionic)
For instruction 7
1
sudo certbot certonly --standalone
Copied!
After filling in details you will get a result like this
1
IMPORTANT NOTES:
2
- Congratulations! Your certificate and chain have been saved at:
3
/etc/letsencrypt/live/simpleweb.example.com/fullchain.pem
4
Your key file has been saved at:
5
/etc/letsencrypt/live/simpleweb.example.com/privkey.pem
6
Your cert will expire on 2021-01-07. To obtain a new or tweaked
7
version of this certificate in the future, simply run certbot
8
again. To non-interactively renew *all* of your certificates, run
9
"certbot renew"
10
- If you like Certbot, please consider supporting our work by:
11
12
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
13
Donating to EFF: https://eff.org/donate-le
Copied!
simpleweb.example.com should be your domain

Create cert.pfx

To create a pfx file that SimpleWebTransport can use run this command in the /etc/letsencrypt/live/simpleweb.example.com/ folder
1
openssl pkcs12 -export -out cert.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem
Copied!
You will be asked for a password, you can set a password or leave it blank.
You might need to be super user in order to do this:
1
su
2
3
cd /etc/letsencrypt/live/simpleweb.example.com/
Copied!
Note: Currently the mono version shipped with unity is unable to load pfx files generated by OpenSSL version 3. You will have to add the -legacy command line argument to the openssl command above to generate a compatible pfx file.

Using cert.pfx

You can either copy the cert.pfx file to your server folder or create a symbolic link
Move
1
mv /etc/letsencrypt/live/simpleweb.example.com/cert.pfx ~/path/to/server/cert.pfx
Copied!
Symbolic link
1
ln -s /etc/letsencrypt/live/simpleweb.example.com/cert.pfx ~/path/to/server/cert.pfx
Copied!

create cert.json file

Create a cert.json that SimpleWebTransport can read
Run this command in the ~/path/to/server/ folder
If you left the password blank at cert creation:
1
echo '{ "path":"./cert.pfx", "password": "" }' > cert.json
Copied!
If you set up a password "yourPassword" at cert creation:
1
echo '{ "path":"./cert.pfx", "password": "yourPassword" }' > cert.json
Copied!

Run your server

After the cert.json and cert.pfx are in the server folder like this
1
ServerFolder
2
|- demo_server.x86_64
3
|- cert.json
4
|- cert.pfx
Copied!
Then make the server file executable
1
chmod +x demo_server.x86_64
Copied!
To run in the active terminal use
1
./demo_server.x86_64
Copied!
To run in background use
1
nohup ./demo_server.x86_64 &
Copied!
nohup means: the executable will keep running after you close your ssh session the & sign means: that your server will run in background
you may need to use sudo to run if you created a symbolic link

Connect to your game

Test everything is working by connection using the editor or a build
set your domain (eg simpleweb.example.com) in the hostname field and then start a client

Debugging

To check if your pfx file is working outside of unity you can use pfxTestServer.js.
To use this install nodejs then set the pfx path and run it with node pfxTestServer.js
You should then be able to visit https://simpleweb.example.com:8000 and have the server response (change port and domain to fit your needs)