on("message", function (array $msg) use ($stream, $jsonProto) { if (array_is_list($msg)) { if (reset($msg) == 'upgrade') { $stream->send([ 'OK', 'Upgrading to JSON protocol']); $stream->upgrade($jsonProto); $stream->send([ 'info' => 'Now using JSON proto' ]); } else { $stream->send([ 'echo', json_encode($msg)]); } } else { $stream->send([ 'echo' => $msg ]); } });