Pusher - 私人频道订阅

时间:2014-05-26 11:10:49

标签: javascript php pusher

我有一个订阅私有频道的代码,当我尝试订阅时,我有下一条消息:

Pusher:无法从您的网络应用程序获取身份验证信息:404

情境:

Javascript(Sencha touch)和PHP(Laravel)

订阅是javascript:

    Pusher.channel_auth_endpoint = "/pusher.php";

    var APP_KEY = '4324523452435234523';
    var pusher = new Pusher(APP_KEY);
    var channel = pusher.subscribe('private-l2');
    channel.bind('pusher:subscription_succeeded', function() {
      alert("ahora siiii");
    });

    // for debugging purposes. Not required.
    Pusher.log = function(msg) {
      if(window.console && window.console.log) {
        window.console.log("PUSHER LOG: "+msg);
      }  
    }

和pusher.php / LARAVEL

    $this->app_id = '66981';
    $this->app_key = '4324523452435234523';
    $this->app_secret = 'f34632459911e2670dcf';

   $pusher = new Pusher($this->app_key, $this->app_secret, $this->app_id);
    $auth    = $pusher->socket_auth(Input::get('channel_name'), Input::get('socket_id'));

    echo $auth;

结果是错误:

     Pusher : State changed : connecting -> connected
     Pusher : Couldn't get auth info from your webapp : 404 

4 个答案:

答案 0 :(得分:2)

私人推送器通道要求客户端进行身份验证以进行访问。有关http://pusher.com/docs/authenticating_usersconfiguring the client for authentication的详细信息,请参阅setting up an authentication endpoint

答案 1 :(得分:2)

您应该为Pusher身份验证设置路由 Route::post('pusher/auth', 'ApiController@pusherAuth');

在该方法中,您应首先禁用php debugbar(如果您正在使用它)对用户进行身份验证,如果身份验证检查,则返回响应。

我会在下面粘贴我的控制器代码。

public function pusherAuth()
{
    \Debugbar::disable();
    $user = auth()->user();

    if ($user) {
        $pusher = new \Pusher(config('broadcasting.connections.pusher.key'), config('broadcasting.connections.pusher.secret'), config('broadcasting.connections.pusher.app_id'));
        echo $pusher->socket_auth(request()->input('channel_name'), request()->input('socket_id'));
        return;
    }else {
        header('', true, 403);
        echo "Forbidden";
        return;
    }
}

我的JS代码:

        var pusher = new Pusher(project.pusherKey, {
            cluster: 'eu',
            encrypted: true,
            authEndpoint: apiUrl(['pusher', 'auth']), // just a helper method to create a link
            auth: {
                headers: {
                    'X-CSRF-Token': project.token // CSRF token
                }
            }
        });

        var channelName =  'private-notifications-' + project.userId; // channel for the user id
        var channel = pusher.subscribe(channelName);

        channel.bind('new_notification', function (data)
        {
            app.addNotification(data); // send the notification in the JS app
        });

我希望这会有所帮助。

干杯!

答案 2 :(得分:1)

更改

Pusher.channel_auth_endpoint = "/pusher.php";

有:

Pusher.channel_auth_endpoint = "/public/broadcasting/auth";

答案 3 :(得分:0)

我不是laravel的专家,但我猜你已经使用了get请求来检索数据(套接字ID和通道名称),而它是从推送服务器到服务器端点的发布请求。使用post检索数据。