如何在CodeIgniter中记录Pusher PHP事件触发器失败?

时间:2017-01-05 14:13:32

标签: php codeigniter pusher

所以我在使用CodeIgniter在wamp服务器上使用Pusher时遇到了问题。客户端工作正常,但PHP不会分解事件。 PHP会对通道进行身份验证,但不会触发事件。如果我使用Pusher事件创建者客户获得事件。

这是代码:

function send_event($channel, $event, $message, $socket_id = NULL){
    $channel_array = array();
    array_push($channel_array, $channel);
    $data['message'] = $message;
    if(is_null($socket_id)){
        $response = $this->pusher->trigger($channel_array, $event, $data);
    }else{
        $response = $this->pusher->trigger($channel_array, $event, $data, $socket_id);
    }
    var_dump($response);        
}

var_dump返回FALSE。我想更多地了解这个问题,但我仍然坚持如何调试。来自推送PHP文档

class MyLogger {
  public function log( $msg ) {
    print_r( $msg . "\n" );
  }
}

$pusher->set_logger( new MyLogger() );

但如果我使用此代码,我会收到CI错误。我尝试在我的Pusher库中使用myLogger,如下所示:

function __construct()
    {
        class MyLogger {
          public function log( $msg ) {
            print_r( $msg . "\n" );
          }
        }

        $this->CI =& get_instance();
        $this->CI->load->library('tank_auth');

        require_once( 'Pusher.php' );

        $this->options['encrypted'] = true;
        $this->pusher = new Pusher(
            $this->key,
            $this->secret,
            $this->app_id,
            $this->options
        );
        $logger= new MyLogger;
        $this->pusher->set_logger( $logger );
    }

    function send_event($channel, $event, $message, $socket_id = NULL){
        $channel_array = array();
        array_push($channel_array, $channel);
        $data['message'] = $message;
        if(is_null($socket_id)){
            $response = $this->pusher->trigger($channel_array, $event, $data);
        }else{
            $response = $this->pusher->trigger($channel_array, $event, $data, $socket_id);
        }
        var_dump($response);        
    }

但得到了错误:

Class declarations may not be nested

1 个答案:

答案 0 :(得分:0)

function __construct()
{
    class MyLogger {
      public function log( $msg ) {
        print_r( $msg . "\n" );
      }
    }
}

这是无效的代码,你不能在函数或其他类中声明一个类。

必须在gobal范围或命名空间中声明:

// declare first class
class MyLogger {
    public function log( $msg ) {
        print_r( $msg . "\n" );
    }
}

// now you can instantiate the class inside you second class
class PusherOrWhatever {
    public function __construct() {   
        $this->pusher->set_logger( new MyLogger );
    }
}