Connect a WebSocket in Cocos Creator TypeScript, handle open, message, error and close events, send JSON, and parse string, ArrayBuffer, and Blob payloads.
Connect and Handle WebSocket Events
Pay attention to the data format expected by the server and deserialize incoming payloads according to their type.
In production, validate parsed messages, catch malformed JSON, use wss://, authenticate the connection, and implement an appropriate reconnect strategy.