Ping Service
Ping Service
获取一个游戏下所有启用的游戏地域的端口信息,用于ping测试延迟。
授权
使用 Basic Authorization 来进行鉴权 (推荐服务器端程序使用). 步骤如下:
在 UOS 网站上获取当前需要使用的 UOS APP 的 AppId 和 AppServiceSecret
注:此处 AppId 和 AppServiceSecret,可在 UOS 网站上获取


在请求任意 API 的 Request Header 中增加 Header:
- Authorization: Basic base64(appId:appServiceSecret)
示例代码
C## C# 使用示例 - Basic 授权方式 using System; using System.Collections.Generic; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; class Program { private static string GetBasicAuthorization(string appId, string appServiceSecret) { string credentials = $"{appId}:{appServiceSecret}"; byte[] credentialsBytes = Encoding.UTF8.GetBytes(credentials); string encodedCredentials = Convert.ToBase64String(credentialsBytes); return $"Basic {encodedCredentials}"; } public static async Task Main(string[] args) { using (HttpClient client = new HttpClient()) { // 使用项目的 APP_ID, APP_SERVICE_SECRET 获取 headers client.DefaultRequestHeaders.Add("Authorization", GetBasicAuthorization(APP_ID, APP_SERVICE_SECRET)); // 以get方法为例,替换 url 为你需要请求的 url HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); // 其他处理…… } } }Python# Python 使用示例 - Basic 授权方式 import base64 import requests def get_basic_authorization(app_id, app_service_secret): """ 获取basic auth的Header """ credentials = f'{app_id}:{app_service_secret}' encoded_credentials = base64.b64encode(credentials.encode("utf-8")).decode("utf-8") return {'Authorization': f'Basic {encoded_credentials}'} # 使用 APP_ID, APP_SERVICE_SECRET 获取Basic Token Header headers = get_basic_authorization(APP_ID, APP_SERVICE_SECRET) # 以get方法为例,替换 url 为你需要请求的 url response = requests.get(url, headers=headers)JavaScript// JavaScript 使用示例 - Basic 授权方式 const axios = require('axios'); function getBasicAuthorization(appId, appServiceSecret) { /** 获取 basic auth 的 Header */ const credentials = `${appId}:${appServiceSecret}`; const encodedCredentials = btoa(credentials); return { 'Authorization': `Basic ${encodedCredentials}` }; } // 使用 APP_ID, APP_SERVICE_SECRET 获取Basic Token Header const headers = getBasicAuthorization(APP_ID, APP_SERVICE_SECRET); // 以get方法为例,替换 url 为你需要请求的 url const response = await axios.get(url, { headers });Go// Go 使用示例 - Basic 授权方式 import ( "bytes" "encoding/base64" "encoding/json" "fmt" "io" "net/http" ) func GetBasicAuthorization(appID, appServiceSecret string) string { credentials := appID + ":" + appServiceSecret encodedCredentials := base64.StdEncoding.EncodeToString([]byte(credentials)) return "Basic " + encodedCredentials } func main() { // 以get方法为例,替换 url 为你需要请求的 url req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } // 使用 APP_ID, APP_SERVICE_SECRET 获取Basic Token Header req.Header.Set("Authorization", GetBasicAuthorization(APP_ID, APP_SERVICE_SECRET)) client := &http.Client{} response, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() // ……其他处理 }
API 列表
Ping Service
基本信息
| URL | Method |
|---|---|
https://multiverse.scaling.unity.cn/v1/game/games/{appId}/ping-services | GET |
输出示例
{
"gameId": "string",
"pingServiceEndpoints": [
{
"regionId": "string",
"httpEndpoint": "string",
"httpPort": 0,
"udpEndpoint": "string",
"udpPort": 0
}
]
}测试示例
通过 Ping Service 获取到节点列表后,以 C# 为例, 我们可以通过以下示例程序来测试客户端与服务器之间的延时。
/// 将下述代码中的 UDP Endpoint / UDP Port 替换成 Ping Service 中获取到的对应的地域的 udpEndpoint / udpPort
/// 该检测方法也可以直接用于游戏逻辑中用于检测游戏与服务器之间的延时
/// 同一地域的 udpEndpoint / udpPort 是固定的, 可以在游戏代码中直接使用已经获取到的 IP/Port
using System.Net;
using System.Net.Sockets;
using System.Text;
//将 UDP Endpoint 替换成 getPingService 获取到到 udpEndpoint
string address = "UDP Endpoint";
if (args.Length > 0)
address = args[0];
//将 UDP Port 替换成 getPingService 获取到到 udpPort
int port = UDP Port;
if (args.Length > 1)
port = int.Parse(args[1]);
var udpClient = new UdpClient(11000);
try
{
for (;;)
{
udpClient.Connect(address, port);
var sendBytes = Encoding.ASCII.GetBytes(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
udpClient.Send(sendBytes, sendBytes.Length);
var remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
var receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
var returnData = Encoding.ASCII.GetString(receiveBytes);
var timestamp = long.Parse(returnData);
var latency = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - timestamp;
Console.WriteLine($"Latency: {latency}ms");
Thread.Sleep(1000);
}
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}