public function onOpen(ConnectionInterface $from, RequestInterface $request = null)
{
echo "New HTTP connection!\n";
//Variables in URLs are not supported in Ratchet for now
//See https://github.com/cboden/Ratchet/pull/143
$requestPath = $request->getPath();
$pathParts = explode('/', preg_replace('#^/peerjs/#', '', $requestPath));
//Remove /peerjs
$action = array_pop($pathParts);
$peerToken = array_pop($pathParts);
$peerId = array_pop($pathParts);
$key = array_pop($pathParts);
$respStatus = 200;
$respHeaders = array('X-Powered-By' => \Ratchet\VERSION, 'Access-Control-Allow-Origin' => '*');
$respBody = null;
switch ($action) {
case 'id':
$respHeaders['Content-Type'] = 'text/html';
if ($peerId === null) {
do {
$peerId = substr(sha1(uniqid('', true) . mt_rand()), 0, self::PEERID_LENGTH);
} while ($this->peerServer->peerIdExists($peerId));
}
$respBody = $peerId;
break;
case 'peers':
if (self::ALLOW_DISCOVERY) {
$peers = $this->peerServer->listPeers();
$list = array();
foreach ($peers as $peer) {
$list[] = $peer['id'];
}
$respBody = $list;
} else {
$respStatus = 401;
// Access denied
}
break;
case 'offer':
case 'candidate':
case 'answer':
case 'leave':
//TODO: start streaming?
//TODO: start streaming?
default:
$respStatus = 400;
//Bad request
}
if (is_array($respBody)) {
// Encode to JSON
$respHeaders['Content-Type'] = 'application/json';
$respBody = json_encode($respBody);
}
//Send response
$response = new Response($respStatus, $respHeaders, (string) $respBody);
$from->send((string) $response);
$from->close();
}