实时消息
实时消息属于 联机大厅 的子能力,用于房间内玩家之间发送自定义数据。Unity 侧提供可靠通道和大消息分片;Godot 当前提供 JSON 消息入口。实时消息需要先完成房间连接,不能在未进入房间时单独使用。
| 引擎 | 状态 | 说明 |
|---|---|---|
| Unity | Preview | 支持房间消息、可靠传输和大消息分片。 |
| 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}"); }}GameMaker 当前暂不支持实时消息。
using Godot;using IGP.GodotSDK.Autoload;
public partial class ChatPanel : Control{ private IGPGodotAutoload IGP => GetNode<IGPGodotAutoload>("/root/IGP");
public override void _Ready() { IGP.MessageReceived += OnMessageReceived; }
public async void SendChat() { await IGP.SendMessageJsonAsync( "chat.message", "{\"text\":\"hello\"}", reliable: true); }
private void OnMessageReceived( string messageType, string roomId, string playerId) { GD.Print($"Message {messageType} from {playerId}"); }}| 引擎 | 检查点 |
|---|---|
| Unity | 两个客户端在同一房间内,一端发送后另一端收到 onMessageReceived。 |
| GameMaker | 当前不做实时消息验收。 |
| Godot | 发送 JSON 消息后,另一端收到 MessageReceived signal。 |
常见问题 / 排查
Section titled “常见问题 / 排查”- 发送成功但未收到消息:确认两个客户端在同一个房间。
- 内容解析失败:先用最简单的 JSON 字符串验证。
- Unity 大消息发送失败:优先使用可靠消息入口。
- Godot 报保留类型错误:换成业务自定义消息类型名。