2015-12-28 60 views
2

我希望将PubNub添加到我的聊天服务器以允许实时发送和接收邮件。目前,该服务器采用PHP作为一系列switch-case操作构建而成。将PubNub添加到聊天服务器

然而,简单地把实例化和订阅服务器的顶部:

$pubnub = new Pubnub(
    "key", ## PUBLISH_KEY 
    "key" ## SUBSCRIBE_KEY 
); 

// Subscribing to the main server channel 

$pubnub->subscribe('MAIN_SERVER', function($message) { 
    //var_dump($message); ## Print Message 
    return true;   ## Keep listening (return false to stop) 
}); 

.... 

switch($action) 
{ 

    // Complete: 
    case "userLogin": 
     //error_log($username,0,"error.log"); 
     if ($userId = authenticateUser($db, $username, $password, $gcmregid)) 
     { 
      // Then they are a user, so yes, then in app, will call the "syncWithServer" action case 
      $out = json_encode(array('response' => SUCCESSFUL)); 

     } 
     else 
.... 

导致服务器超时:

PHP Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\Server\lib\Pubnub\Clients\DefaultClient.php on line 30 

如何PubNub被集成到我现在的服务器?

回答

1

PHP订阅循环PubNub

这是一个阻塞呼叫。您需要在Web服务器环境之外运行此方法。相反,您需要在命令行中运行脚本。你也将要监视使用upstart或类似的系统级这个过程

## Process Messages 
function receive_and_process($message) { 
    switch($messge->action) { ... } 
} 

## This is BLOCKING 
$pubnub->subscribe('MAIN_SERVER', function($message) { 
    receive_and_process($message); 
    return true; 
}); 

你的启动命令将php my-php-server.php