[Unity Mirror] WebSockets 传输

使用 websocket 协议的传输。这允许在unity的 WebGL 构建中使用此传输。

在这里插入图片描述


Logging

可以使用传输上的下拉菜单或设置 Mirror.SimpleWeb.Log.level 来设置日志级别。

传输在其 Awake 和 OnValidate 方法中应用下拉值。

Log方法
此传输中的日志方法使用 ConditionalAttribute,因此根据预处理器定义将它们删除。

这些预处理器定义了日志记录的效果

  • DEBUG 允许警告/错误日志
  • SIMPLEWEB_LOG_ENABLED 允许所有日志

没有 SIMPLEWEB_LOG_ENABLED 信息或详细日志记录将永远不会发生,即使日志级别允许。


设置 SSL

如果您在 https 域上托管 webgl 构建,则需要使用需要 ssl 证书的 wss。

预设置

  • 你需要一个域名
    • dns 记录指向云服务器
  • 设置云服务器:如何设置谷歌云服务器

注意:您可能需要为 certbot 打开端口 80

获取证书

遵循此处的指南:

https://letsencrypt.org/getting-started/ https://certbot.eff.org/instructions

查找您的服务器版本的说明,下面是 Ubuntu 18.04 LTS(仿生)的链接

https://certbot.eff.org/lets-encrypt/ubuntubionic-other

对于指令 7

sudo certbot certonly --standalone

填写详细信息后会得到这样的结果

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/simpleweb.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/simpleweb.example.com/privkey.pem
   Your cert will expire on 2021-01-07. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

simpleweb.example.com 应该是您的域名

创建 cert.pfx

要创建 SimpleWebTransport 可以使用的 pfx 文件,请在 /etc/letsencrypt/live/simpleweb.example.com/ 文件夹中运行此命令

openssl pkcs12 -export -out cert.pfx -inkey privkey.pem -in cert.pem -certfile chain.pem

系统会要求您输入密码,您可以设置密码或将其留空。
您可能需要成为超级用户才能执行此操作:

su

cd /etc/letsencrypt/live/simpleweb.example.com/

使用 cert.pfx

您可以将 cert.pfx 文件复制到服务器文件夹或创建符号链接
移动

mv /etc/letsencrypt/live/simpleweb.example.com/cert.pfx ~/path/to/server/cert.pfx

符号链接

ln -s /etc/letsencrypt/live/simpleweb.example.com/cert.pfx ~/path/to/server/cert.pfx

创建 cert.json 文件

创建一个 SimpleWebTransport 可以读取的 cert.json
在 ~/path/to/server/ 文件夹中运行此命令
如果您在创建证书时将密码留空:

echo '{ "path":"./cert.pfx", "password": "" }' > cert.json

如果您在创建证书时设置了密码“yourPassword”:

echo '{ "path":"./cert.pfx", "password": "yourPassword" }' > cert.json

运行你的服务器

在 cert.json 和 cert.pfx 像这样在服务器文件夹中之后

ServerFolder
|- demo_server.x86_64
|- cert.json
|- cert.pfx

然后使服务器文件可执行

chmod +x demo_server.x86_64

要在活动终端中运行,请使用

./demo_server.x86_64

在后台运行

nohup ./demo_server.x86_64 &

nohup 表示:关闭 ssh 会话后可执行文件将继续运行 & 符号表示:您的服务器将在后台运行

如果您创建了符号链接,则可能需要使用 sudo 运行

连接到您的游戏

使用Editor或Build通过连接测试一切是否正常

在主机名字段中设置您的域(例如 simpleweb.example.com),然后启动客户端

调试

要检查您的 pfx 文件是否在Unity之外工作,您可以使用 pfxTestServer.js。
要使用此安装 nodejs,请设置 pfx 路径并使用 node pfxTestServer.js 运行它
然后您应该能够访问 https://simpleweb.example.com:8000 并获得服务器响应(更改端口和域名以满足您的需要)