跳转到内容

实时消息

实时消息属于 联机大厅 的子能力,用于房间内玩家之间发送自定义数据。Unity 侧提供可靠通道和大消息分片;Godot 当前提供 JSON 消息入口。实时消息需要先完成房间连接,不能在未进入房间时单独使用。

引擎状态说明
UnityPreview支持房间消息、可靠传输和大消息分片。
GameMaker暂不支持当前 GameMaker 首期不包含实时消息。
Godot开发中预览 runtime 已有 JSON 消息发送和接收。
  • 已完成 房间 的最小流程。
  • 已进入房间。
  • 消息类型不要使用 SDK 保留类型。
  • 发送的内容应保持结构稳定,便于两端解析。
using System;
using UnityEngine;
using IGP.UnitySDK;
using IGP.UnitySDK.Models;
public sealed class IGPChatDriver : MonoBehaviour
{
[SerializeField] private IGPRuntimeManager runtimeManager;
private void OnEnable()
{
runtimeManager.onMessageReceived.AddListener(OnMessageReceived);
}
private void OnDisable()
{
runtimeManager.onMessageReceived.RemoveListener(OnMessageReceived);
}
public async void SendChat()
{
await runtimeManager.SendMessageAsync(new Message
{
type = "chat.message",
roomId = runtimeManager.CurrentRoomId,
playerId = runtimeManager.PlayerId,
reliable = true,
content = new
{
text = "hello",
sentAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
},
});
}
private void OnMessageReceived(string messageType, object content)
{
Debug.Log($"Message {messageType}: {content}");
}
}
引擎检查点
Unity两个客户端在同一房间内,一端发送后另一端收到 onMessageReceived
GameMaker当前不做实时消息验收。
Godot发送 JSON 消息后,另一端收到 MessageReceived signal。
  • 发送成功但未收到消息:确认两个客户端在同一个房间。
  • 内容解析失败:先用最简单的 JSON 字符串验证。
  • Unity 大消息发送失败:优先使用可靠消息入口。
  • Godot 报保留类型错误:换成业务自定义消息类型名。