I had the same problem. The problem is reading Flash data from the socket.
The fact is that the Flash ProgressEvent.SOCKET_DATA event is triggered even if the server did not send all the data and something remains (especially when the data is large and the connection is slow). So, something like {"key": "value"} comes in two (or more) parts, for example: {"key": "val and ue"} . Also sometimes you can get several attached JSON in one message, for example {"json1key": "value"} {"json2key": "value"} - the built-in Flash JSON parser will also not be able to process them.
To combat this, I recommend that you modify the SocketData handler in your Flash application to add a cache for the received strings. Like this:
// declaring vars private var _socket:Socket; private var _cache: String = ""; // adding EventListener _socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); private function onSocketData(e: Event):void { // take the incoming data from socket var fromServer: ByteArray = new ByteArray; while (_socket.bytesAvailable) { _socket.readBytes(fromServer); } var receivedToString: String = fromServer.toString(); _cache += receivedToString; if (receivedToString.length == 0) return; // nothing to parse // convert that long string to the Vector of JSONs // here is very small and not fail-safe alghoritm of detecting separate JSONs in one long String var jsonPart: String = ""; var jsonVector: Vector.<String> = new Vector.<String>; var bracketsCount: int = 0; var endOfLastJson: int = 0; for (var i: int = 0; i < _cache.length; i++) { if (_cache.charAt(i) == "{") bracketsCount += 1; if (bracketsCount > 0) jsonPart = jsonPart.concat(_cache.charAt(i)); if (_cache.charAt(i) == "}") { bracketsCount -= 1; if (bracketsCount == 0) { jsonVector.push(jsonPart); jsonPart = ""; endOfLastJson = i; } } } // removing part that isn't needed anymore if (jsonVector.length > 0) { _cache = _cache.substr(endOfLastJson + 1); } for each (var part: String in jsonVector) { trace("RECEIVED: " + part); // voila! here is the full received JSON } }
source share