public function onCall(Conn $from, $callId, $topic, array $params)
 {
     echo 'Received data from ' . $from->resourceId . "\n";
     if ($topic->getId() == 'api.call') {
         if (!isset($params['id']) || !is_int($params['id'])) {
             var_dump($params);
             $from->callError($callId, $topic, 'Bad request: invalid call id');
             return;
         }
         // Handle HTTP headers
         if (isset($params['http_headers']) && is_array($params['http_headers'])) {
             if (isset($params['http_headers']['Accept-Language'])) {
                 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $params['http_headers']['Accept-Language'];
             }
         }
         try {
             if (isset($params['groupped']) && $params['groupped'] == true) {
                 $resp = $this->_handleRequestGroup($from, $params['data']);
             } else {
                 $resp = $this->_handleRequest($from, $params['data']);
             }
         } catch (Exception $e) {
             $errMsg = $e->getMessage();
             $resp = new ApiResponse();
             $resp->setSuccess(false);
             $resp->setValue($errMsg);
             $resp->setChannel(2, $errMsg);
         }
         $resp->setId($params['id']);
         $respData = $resp->generateArray();
         echo 'Sending data to ' . $from->resourceId . "\n";
         $from->callResult($callId, $respData);
     } else {
         $from->callError($callId, $topic, 'Bad request: unsupported topic "' . $topic->getId() . '"');
     }
 }
 public function onMessage(ConnectionInterface $from, $msg)
 {
     echo 'Received data from ' . $from->resourceId . ' (' . strlen($msg) . ')' . "\n";
     try {
         $req = json_decode($msg, true);
         if (json_last_error() !== JSON_ERROR_NONE || empty($req)) {
             throw new \RuntimeException('Bad request: invalid JSON (#' . json_last_error() . '): ' . $input);
         }
         if (!isset($req['id']) || !is_int($req['id'])) {
             throw new \RuntimeException('Bad request: invalid request id');
         }
         if (!isset($req['data']) || !is_array($req['data'])) {
             throw new \RuntimeException('Bad request: invalid request data');
         }
         if (isset($req['http_headers']) && is_array($req['http_headers'])) {
             if (isset($req['http_headers']['Accept-Language'])) {
                 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $req['http_headers']['Accept-Language'];
             }
         }
         $reqId = $req['id'];
         if (isset($req['groupped']) && $req['groupped'] == true) {
             $resp = $this->_handleRequestGroup($from, $req['data']);
         } else {
             $resp = $this->_handleRequest($from, $req['data']);
         }
         $resp->setId($reqId);
     } catch (Exception $e) {
         $errMsg = $e->getMessage();
         $resp = new ApiResponse();
         $resp->setSuccess(false);
         $resp->setValue($errMsg);
         $resp->setChannel(2, $errMsg);
     }
     $output = $resp->generate();
     echo 'Sending data to ' . $from->resourceId . ' (' . strlen($output) . ')' . "\n";
     $from->send($output);
 }