快速开始部署游戏后端
你可以下载我们针对 C++ 和游戏的示例项目:
git clone https://github.com/leancloud/cpp-socket-demo.git
这个示例中包含一个简单的实时多人游戏,我们先在本地启动这个项目:
$ make && ./cpp-socket
[UDP] Socket is listening on 4000
[TCP] Socket is listening on 4000
[HTTP] Socket is listening on 3000
用 nc
连接其 TCP 端口的程序:
$ nc 127.0.0.1 4000
>> 1 players online, last number is 1, you must send the next number but skip numbers which multiples of 3 or includes the digit 3.
2 (your input)
>> 2 players online, 127.0.0.1:59385 sent 2.
>> 127.0.0.1:59897 lost, 2 players online.
4 (your input)
>> 1 players online, 127.0.0.1:59385 sent 4.
在这个游戏中,你需要向服务端发送下一个数字,但要跳过 3 的倍数或包含数字 3 的数字,如果输入错误会被断开连接。
部署到云引擎
选择应用和分组:
tds switch
部署到生产环境:
tds deploy --prod
更多有关命令行工具和本地调试的内容请看 云引擎命令行工具使用指南。
连接到云端游戏
发请求获取连接地址(需要替换其中的 appId、分组名、生产环境或预备环境):
$ curl -H 'X-LC-Id: SJjoXHWuhewHKV4Ojw' 'https://shared.cloud.tds1.tapapis.cn/1.1/engine/gateway/route?groupName=udp&prod=1'
[
{
"name": "game",
"protocol": "tcp",
"publicPort": 19766,
"publicIp": "106.75.70.96"
}
]
然后用 nc
连接即可:
$ nc 106.75.70.96 19766
信息
每当云引擎实例部署、重启或被自动调度,IP 和端口都有可能发生变化,因此请在每次连接前都从服务器查询最新的 IP 和端口。
更多
接下来可以查看 游戏后端参考架构 来了解在云引擎上运行游戏后端的推荐架构,或者在 游戏后端开发指南 中了解详细的接入方法。
你还可以查看 云引擎平台功能 来了解云引擎提供的更多功能,或查看专门的页面来了解具体运行环境的详情: