我想将PubNub添加到我的聊天服务器中,以便实时发送和接收消息。目前,服务器是在PHP中作为一系列switch-case
操作构建的。
然而,只需将实例化和订阅添加到服务器的顶部:
$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集成到我现在的服务器中?
使用PubNub的PHP订阅循环
这是一个阻止呼叫。您需要在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
。