xiaohongshu
xiaohongshu
76-webgl-ws-polling-bridge
Status: ACTIVE AppliesTo: v10 Type: Policy / Contract
Full skill instructions
Status: ACTIVE AppliesTo: v10 Type: Policy / Contract
WebGL에서 WebSocket 이벤트를 폴링(Polling) 기반으로 전달하는 .jslib ↔ C# 간 계약을 정의한다.
이 문서는 인터페이스 계약이 핵심이며, 구현 코드가 정답이다.
SendMessage/콜백 기반 설계 금지
SendMessage 콜백을 사용하지 않는다.WS_PollEvent로만 소비한다.이벤트 소비는 Tick()에서만 수행
Tick() 호출 시에만 이벤트 큐를 drain한다.이벤트 처리량 제한 필수
Tick()에서 최대 N개의 이벤트만 처리한다.아래 함수들은 .jslib에서 구현하고, C#에서 [DllImport("__Internal")]로 호출한다.
[DllImport("__Internal")]
private static extern int WS_Connect(string url, string subProtocolsJson);
url: WebSocket URL (wss:// 필수, WebGL에서 ws:// 금지)subProtocolsJson: sub-protocol 배열의 JSON 문자열 (nullable, 예: ["devian.v1"])socketId (0 이상), 실패 시 음수[DllImport("__Internal")]
private static extern int WS_GetState(int socketId);
0=CONNECTING, 1=OPEN, 2=CLOSING, 3=CLOSED, -1=INVALID[DllImport("__Internal")]
private static extern int WS_SendBinary(int socketId, IntPtr ptr, int len);
ptr: WASM heap 상의 버퍼 포인터 (GCHandle.Alloc(Pinned))len: 버퍼 길이0=성공, 음수=실패[DllImport("__Internal")]
private static extern void WS_Close(int socketId, int code, string reason);
code: 종료 코드 (예: 1000)reason: 종료 사유 (nullable)[DllImport("__Internal")]
private static extern int WS_PollEvent(
int socketId,
out int eventType,
out int code,
out IntPtr dataPtr,
out int dataLen,
out IntPtr messagePtr);
0 = 이벤트 없음1 = 이벤트 있음 (출력값 유효)eventType: 이벤트 타입 (아래 참조)code: CLOSE 이벤트의 종료 코드, ERROR/MESSAGE에서는 0dataPtr: MESSAGE 이벤트의 바이너리 데이터 포인터 (_malloc 할당)dataLen: MESSAGE 이벤트의 데이터 길이messagePtr: ERROR/CLOSE 이벤트의 UTF8 문자열 포인터 (_malloc 할당, nullable)[DllImport("__Internal")]
private static extern void WS_FreeBuffer(IntPtr ptr);
dataPtr 해제[DllImport("__Internal")]
private static extern void WS_FreeString(IntPtr ptr);
messagePtr 해제| 값 | 이름 | 설명 |
|---|---|---|
| 0 | NONE | 이벤트 없음 (리턴값 0과 동일) |
| 1 | OPEN | 연결 성공 |
| 2 | CLOSE | 연결 종료 (code, messagePtr 유효) |
| 3 | ERROR | 오류 발생 (messagePtr 유효) |
| 4 | MESSAGE | 바이너리 메시지 수신 (dataPtr, dataLen 유효) |
// Tick() 내부
const int MaxEventsPerTick = 64;
for (int i = 0; i < MaxEventsPerTick; i++)
{
int result = WS_PollEvent(_socketId, out int eventType, out int code,
out IntPtr dataPtr, out int dataLen, out IntPtr messagePtr);
if (result == 0)
break; // 이벤트 없음
switch (eventType)
{
case 1: // OPEN
EnqueueDispatch(() => OnOpen?.Invoke());
break;
case 2: // CLOSE
string reason = messagePtr != IntPtr.Zero
? Marshal.PtrToStringUTF8(messagePtr)
: "";
EnqueueDispatch(() => OnClose?.Invoke((ushort)code, reason));
if (messagePtr != IntPtr.Zero) WS_FreeString(messagePtr);
break;
case 3: // ERROR
string errorMsg = messagePtr != IntPtr.Zero
? Marshal.PtrToStringUTF8(messagePtr)
: "Unknown error";
EnqueueDispatch(() => OnError?.Invoke(new Exception(errorMsg)));
if (messagePtr != IntPtr.Zero) WS_FreeString(messagePtr);
break;
case 4: // MESSAGE
// ArrayPool로 복사 후 core.OnFrame 호출
var buffer = ArrayPool<byte>.Shared.Rent(dataLen);
Marshal.Copy(dataPtr, buffer, 0, dataLen);
_core.OnFrame(_sessionId, buffer.AsSpan(0, dataLen));
ArrayPool<byte>.Shared.Return(buffer);
WS_FreeBuffer(dataPtr);
break;
}
}
WS_PollEvent가 유일한 이벤트 소비 경로이다 (SendMessage 없음)WS_FreeBuffer(dataPtr) 호출됨WS_FreeString(messagePtr) 호출됨xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience