dqv84329 2017-01-05 14:13
浏览 79
已采纳

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

So I have a problems working with Pusher on wamp server using CodeIgniter. Client side works fine, but PHP does not triger events. PHP does authenticate channels, but does not trigger events. If I use Pusher event creator clients get events.

Here is code:

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 return FALSE. I would want to understand the issue more but I am stuck on how to debug. From pusher PHP docs

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

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

But if I use this code I get CI errors. I tried to use myLogger in my Pusher library like this:

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

        $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);        
    }

but got error:

Class declarations may not be nested
  • 写回答

1条回答 默认 最新

  • doubi12138 2017-01-05 14:17
    关注
    function __construct()
    {
        class MyLogger {
          public function log( $msg ) {
            print_r( $msg . "
    " );
          }
        }
    }
    

    This is invalid code, you cant declare a class inside a function or other class.

    It must be declared in gobal scope or namespace:

    // declare first class
    class MyLogger {
        public function log( $msg ) {
            print_r( $msg . "
    " );
        }
    }
    
    // now you can instantiate the class inside you second class
    class PusherOrWhatever {
        public function __construct() {   
            $this->pusher->set_logger( new MyLogger );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择