$body = file_get_contents('php://input');
$incoming_data = json_decode($body, true);
switch($api_method) {
case"connect":
// Save sessionId
saveSessionId($incoming_data['sessionId']);
// POST /connect query to WCS default RoomApp
$response_data = json_decode(postToRoomApp($api_method, $body), true);
break;
case"OnDataEvent":
// Save message
if($incoming_data['payload']['command'] == "sendMessage") {
saveMessage($incoming_data['sessionId'], $incoming_data['payload']);
}
// POST /OnDataEvent query to WCS default RoomApp
$response_data = json_decode(postToRoomApp($api_method, $body), true);
break;
default:
// POST any query to WCS default RoomApp
$response_data = json_decode(postToRoomApp($api_method, $body), true);
break;
}
header('HTTP/1.1 200 OK', true, 200);
header('Content-Type: application/json');
echo json_encode($response_data);
// Passing query to WCS default RoomApp
function postToRoomApp($method, $data_string) {
// Here should be your WSC RoomApp address
$wcs_url="http://wcs:8081/apps/RoomApp/" . $method;
$response="";
if( $curl = curl_init() ) {
curl_setopt($curl, CURLOPT_URL, $wcs_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Content-Length: ' . strlen($data_string)));
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($http_code != 200) {
ubnormalResponse($http_code);
}
error_log($response);
curl_close($curl);
}
return($response);
}
function ubnormalResponse($code) {
if ($code == 403) {
header('HTTP/1.1 403 Forbidden', true, $code);
} else {
header(':', true, $code);
}
die();
}