Let's decribe the case as we see it:We have previously implemented hooks to authenticate the normal webrtc streams but never manage to get that working for the roomAPI. Here is the link of the thread https://forum.flashphoner.com/threads/our-server-is-being-spammed.12098/post-20396
- authenticated users subscribe to the audio only stream
- paid users just play the stream
- free users should listen advertising audio over the main audio when speaker presses a key
- it is desireable the advertising audio to be per user
As we mentioned it that thread, the RoomApi is excessive for your case. One REST hook and one REST API query seems enough:
1. Collect client session Id while authenticating a user with /connect REST hook:
PHP:
<?php
$api_method = array_pop(explode("/", $_SERVER['REQUEST_URI']));
$incoming_data = json_decode(file_get_contents('php://input'), true);
$response_data = $incoming_data;
switch($api_method) {
case"connect":
// Authenticate user as you wish
if (authenticateUser($incoming_data)) {
error_log("User authorized");
} else {
error_log("User not authorized. Connection failed with 403 status.");
ubnormalResponse(403);
}
break;
}
header('Content-Type: application/json');
echo json_encode($incoming_data);
// User authentication
function authenticateUser($incoming_data) {
...
storeSessionId($user_key, $incoming_data['sessionId']);
}
// Store sessionId to some DB
function storeSessionId($user_key, $sessionId) {
...
}
function ubnormalResponse($code) {
if ($code == 403) {
header('HTTP/1.1 403 Forbidden', true, $code);
} else {
header(':', true, $code);
}
die();
}
?>
Bash:
for sessionId in ${sessionIds[@]}; do
curl -H "Content-Type: application/json" -X POST http://wcs:8081/rest-api/data/send -d '{"nodeId":"", "sessionId":"$sessionId", "operationId":"", "payload":{"message":"Play advert"}}'
done
JavaScript:
Flashphoner.createSession(sessionOptions).on(SESSION_STATUS.ESTABLISHED, function(session){
...
}}.on(SESSION_STATUS.APP_DATA, function(data){
if (data.payload.message == "Play advert") {
playAdvert();
}
});