跳转到主要内容

频道列表查询

介绍查询频道列表的两种方法。

WebSocket 方式

通过 WebSocket 查询当前可连接的频道列表。

方法信息

项目内容
方法ChatManager.Instance.GetChannels()
回调OnChannels(ChatChannelInfo[] channels)

ChatChannelInfo

字段类型说明
channelstring频道名称
countint在线人数

使用示例

using UnityEngine;

public class ChatExample : MonoBehaviour, IChatListener
{
public void GetChannels()
{
ChatManager.Instance.GetChannels();
}

public void OnChannels(ChatChannelInfo[] channels)
{
foreach (var channel in channels)
Debug.Log($"Channel: {channel.channel}, Users: {channel.count}");
}

// ... 기타 IChatListener 메서드 구현
}

REST API 方式

可在 Socket 连接前使用

REST API 方式在 WebSocket 连接前也可以使用。如果需要在连接聊天服务器之前预先查询频道列表,请使用此方式。

URL 确认

此 API 使用 service-api.playnanoo.com 域名。

API 信息

  • URL: https://service-api.playnanoo.com/chat/v20221201/channels
  • Method: GET
  • 需要认证: 否

请求参数

Req 类继承自 DeviceInfo

没有额外的参数。

响应数据

Res

字段类型说明
ItemsList<ChatChannelInfo>频道列表

ChatChannelInfo

字段类型说明
channelstring频道名称
countint在线人数

Unity C# 实现

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;

public class Channles
{
static string path = "https://service-api.playnanoo.com/chat/v20221201/channels";

[Serializable]
public class Req : DeviceInfo
{
public IEnumerator Send(Action<Res> onSuccess, Action<BaseResponse> onError)
{
yield return HttpClient.Send<Req, Res>(
UnityWebRequest.kHttpVerbGET,
path,
requireToken: false,
body: this,
onSuccess: onSuccess,
onError: onError
);
}
}

[Serializable]
public class Res : BaseResponse
{
public List<ChatChannelInfo> Items;
}

[Serializable]
public class ChatChannelInfo
{
public string channel;
public int count;
}
}

使用示例

using UnityEngine;

public class ChatExample : MonoBehaviour
{
public void GetChannelList()
{
Channles.Req req = new Channles.Req();
StartCoroutine(req.Send(
onSuccess: res =>
{
foreach(Channles.ChatChannelInfo item in res.Items)
{
Debug.Log($"Channel : {item.channel}");
Debug.Log($"Count : {item.count}");
}
},
onError: (error) =>
{
Debug.LogError($"GetChannelList 실패: [{error.ErrorCode}] {error.Message}");
}
));
}
}