列出 Game Servers 大区。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
using Google.Api.Gax;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Gaming.V1;
using System.Collections.Generic;
using System.Linq;
public class ListRealmsSample
{
public IList<Realm> ListRealms(
string projectId, string regionId)
{
// Create the client.
RealmsServiceClient client = RealmsServiceClient.Create();
ListRealmsRequest request = new ListRealmsRequest
{
ParentAsLocationName = LocationName.FromProjectLocation(projectId, regionId)
};
// Make the request.
PagedEnumerable<ListRealmsResponse, Realm> response = client.ListRealms(request);
// The returned sequence will lazily perform RPCs as it's being iterated over.
return response.ToList();
}
}
Go
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
import (
"context"
"fmt"
"io"
gaming "cloud.google.com/go/gaming/apiv1"
"google.golang.org/api/iterator"
gamingpb "google.golang.org/genproto/googleapis/cloud/gaming/v1"
)
// listRealms lists the realms in a location.
func listRealms(w io.Writer, projectID, location string) error {
// projectID := "my-project"
// location := "global"
ctx := context.Background()
client, err := gaming.NewRealmsClient(ctx)
if err != nil {
return fmt.Errorf("NewRealmsClient: %v", err)
}
defer client.Close()
req := &gamingpb.ListRealmsRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
}
it := client.ListRealms(ctx, req)
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return fmt.Errorf("Next: %v", err)
}
fmt.Fprintf(w, "Realm listed: %v\n", resp.Name)
}
return nil
}
Java
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
import com.google.cloud.gaming.v1.ListRealmsRequest;
import com.google.cloud.gaming.v1.Realm;
import com.google.cloud.gaming.v1.RealmsServiceClient;
import com.google.cloud.gaming.v1.RealmsServiceClient.ListRealmsPagedResponse;
import com.google.common.base.Strings;
import java.io.IOException;
public class ListRealms {
public static void listRealms(String projectId, String regionId) throws IOException {
// String projectId = "your-project-id";
// String regionId = "us-central1-f";
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (RealmsServiceClient client = RealmsServiceClient.create()) {
String parent = String.format("projects/%s/locations/%s", projectId, regionId);
ListRealmsPagedResponse response = client.listRealms(parent);
for (Realm realm : response.iterateAll()) {
System.out.println("Realm found: " + realm.getName());
}
while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
ListRealmsRequest request =
ListRealmsRequest.newBuilder()
.setParent(parent)
.setPageToken(response.getNextPageToken())
.build();
response = client.listRealms(request);
for (Realm realm : response.iterateAll()) {
System.out.println("Realm found: " + realm.getName());
}
}
}
}
}
Node.js
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
const {RealmsServiceClient} = require('@google-cloud/game-servers');
const client = new RealmsServiceClient();
async function listRealms() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const location = 'A Compute Engine region, e.g. "us-central1"';
const request = {
parent: `projects/${projectId}/locations/${location}`,
};
const [results] = await client.listRealms(request);
for (const realm of results) {
console.log(`Realm name: ${realm.name}`);
console.log(`Realm description: ${realm.description}`);
console.log(`Realm time zone: ${realm.timeZone}`);
const createTime = realm.createTime;
const createDate = new Date(createTime.seconds * 1000);
console.log(`Realm created on: ${createDate.toLocaleDateString()}\n`);
}
}
listRealms();
Python
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
def list_realms(project_id, location):
"""Lists the existing realms."""
client = gaming.RealmsServiceClient()
response = client.list_realms(parent=f"projects/{project_id}/locations/{location}")
for realm in response.realms:
print(f"Name: {realm.name}")
return response.realms
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。