惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
小众软件
小众软件
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
B
Blog
量子位
B
Blog RSS Feed
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
Jina AI
Jina AI
C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The GameIdentityRegistry Pattern — Bridging Mirror netId ...
Magithar Sri · 2026-05-03 · via DEV Community

When you run Mirror and Socket.IO in the same Unity project, you immediately hit a translation problem.

Mirror identifies players by netId — a uint assigned at spawn time by the Mirror host. Socket.IO identifies players by playerId — a string assigned by your Node.js backend when they connect.

These two IDs have nothing to do with each other. They're generated by different systems at different times. But when a score_update arrives from your Socket.IO backend with a playerId, you need to find the corresponding Mirror NetworkIdentity to apply the effect. And when a Mirror player spawns, you need to register which playerId they belong to so future backend events can reach them.

GameIdentityRegistry is the pattern that solves this.

The Pattern

A static lookup table. Two dictionaries. One clear API.

public static class GameIdentityRegistry
{
    private static readonly Dictionary<uint, string> _netIdToPlayerId = new();
    private static readonly Dictionary<string, uint> _playerIdToNetId = new();

    public static void Register(uint netId, string playerId)
    {
        _netIdToPlayerId[netId] = playerId;
        _playerIdToNetId[playerId] = netId;
    }

    public static NetworkIdentity GetNetworkObject(string playerId)
    {
        if (!_playerIdToNetId.TryGetValue(playerId, out uint netId))
            return null;

        // Check server first, then client
        if (NetworkServer.spawned.TryGetValue(netId, out var identity))
            return identity;
        if (NetworkClient.spawned.TryGetValue(netId, out identity))
            return identity;

        return null;
    }

    public static string GetPlayerId(uint netId)
    {
        return _netIdToPlayerId.TryGetValue(netId, out string playerId)
            ? playerId : null;
    }

    public static void Clear()
    {
        _netIdToPlayerId.Clear();
        _playerIdToNetId.Clear();
    }
}

Enter fullscreen mode Exit fullscreen mode

GetNetworkObject checks NetworkServer.spawned before NetworkClient.spawned — this ensures it works correctly in all Mirror roles: dedicated server, host, and client.

Registration — When and Where

Registration happens in PlayerIdentityBridge, a NetworkBehaviour attached to the Mirror player prefab.

The registration must happen on all instances — server/host and all clients — because GetNetworkObject might be called on any of them when a backend event arrives.

public class PlayerIdentityBridge : NetworkBehaviour
{
    public override void OnStartLocalPlayer()
    {
        var store = FindObjectOfType<LobbyStateStore>();
        CmdRegisterIdentity(store.LocalPlayerId);
    }

    [Command]
    private void CmdRegisterIdentity(string playerId)
    {
        // Register on server/host
        GameIdentityRegistry.Register(netIdentity.netId, playerId);
        // Propagate to all clients
        RpcRegisterIdentity(netIdentity.netId, playerId);
    }

    [ClientRpc]
    private void RpcRegisterIdentity(uint netId, string playerId)
    {
        GameIdentityRegistry.Register(netId, playerId);
    }
}

Enter fullscreen mode Exit fullscreen mode

The flow: local player spawns → OnStartLocalPlayer fires → CmdRegisterIdentity runs on the server → RpcRegisterIdentity propagates to all clients. Every instance now has the mapping.

Usage — Routing Backend Events

When a Socket.IO event arrives with a playerId, resolve it to a Mirror object and apply the effect:

// In GameEventBridge.Subscribe()
game.On("score_update", (string json) =>
{
    var obj = JObject.Parse(json);
    string playerId = obj["playerId"]?.ToString();
    int score = obj.Value<int>("score");

    var identity = GameIdentityRegistry.GetNetworkObject(playerId);
    if (identity == null) return; // player may have left

    identity.GetComponent<PlayerScore>()?.SetScore(score);
});

game.On("player_killed", (string json) =>
{
    var obj = JObject.Parse(json);
    string victimId = obj["victimId"]?.ToString();

    var identity = GameIdentityRegistry.GetNetworkObject(victimId);
    if (identity == null) return;

    identity.GetComponent<PlayerHealth>()?.Die();
});

Enter fullscreen mode Exit fullscreen mode

The null check on GetNetworkObject is important — a player may have disconnected between when the server sent the event and when it arrived. Always guard.

Cleanup — When and Where

Clear the registry in exactly two places:

// 1. On ReturnToLobby — match ended normally
public void ReturnToLobby()
{
    // ... Mirror shutdown ...
    GameIdentityRegistry.Clear();
    // ... LeaveRoom ...
}

// 2. On Socket.IO disconnect — unexpected disconnection
store.OnDisconnected += () => GameIdentityRegistry.Clear();

Enter fullscreen mode Exit fullscreen mode

Missing either case leaves stale mappings. The next match starts with entries from the previous one, GetNetworkObject returns wrong objects, and events apply to destroyed players. Subtle, intermittent, hard to reproduce.

Why Static?

A static class means no singleton MonoBehaviour, no inspector wiring, no FindObjectOfType. Any component anywhere — GameEventBridge, PlayerIdentityBridge, a HUD script — can call GameIdentityRegistry.Register() or GameIdentityRegistry.GetNetworkObject() without a reference.

This is safe because the registry's lifecycle is explicitly managed: Register() populates it, Clear() resets it. There's no implicit state — you always know exactly what's in it based on what's been registered since the last Clear().

The Broader Pattern

GameIdentityRegistry is a specific instance of a general pattern: an identity bridge between two systems that use incompatible ID schemes.

The same pattern applies anywhere two systems need to reference the same logical entity by different identifiers:

  • Mirror netId ↔ Steam CSteamID
  • Mirror netId ↔ Photon ActorNumber
  • Mirror netId ↔ any backend player ID

The implementation is always the same: two dictionaries, a Register() call on spawn, a lookup on event receipt, a Clear() on session end.


The Full Context

This pattern is part of the Mirror Integration sample in socketio-unity — an open-source Socket.IO v4 client for Unity with full WebGL support.

Install via Package Manager → Add package from git URL:

https://github.com/Magithar/socketio-unity.git?path=/package

Enter fullscreen mode Exit fullscreen mode

Then import via Package Manager → Samples → "Mirror Integration" to see the full working implementation.

MIT licensed. Zero paid dependencies.


Have you needed to bridge IDs between two networking systems before? What was your approach?