I'm using PHP sockets for managing data in a chat application, here's a sample JSON string I'm expecting from the socket read :
{ "m_time" : "2015-04-07 11:37:35", "id" : "29", "msg" : "Hai there. This is a test message"}
But sometimes in the socket reads multiple objects concatenated, like this :
{ "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"}{ "m_time" : "2015-04-07 11:37:36", "id" : "31", "msg" : "Hai there 2"}{ "m_time" : "2015-04-07 11:37:37", "id" : "32", "msg" : "Hai there 3"}
How can I json_decode no matter if there's a single object or multiple ones ?
PHP Code for Socket read:
while(@socket_recv($changed_socket, $buf, READ_SIZE, 0) >= 1)
{
if(!$buf) logResponse('Socket Read Failed for '. $changed_socket);
$received_text = $buf; //unmask data
$tst_msg = json_decode($received_text); //json decode
logResponse('Received Data: '. $received_text);
}
// logResponse() is used to write a log file log.html