簡體   English   中英

Pusher - 私人頻道訂閱

[英]Pusher - Private channel subscription

我有一個訂閱私有頻道的代碼,當我嘗試訂閱時,我有下一條消息:

Pusher:無法從您的webapp獲取身份驗證信息: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 

私有Pusher通道要求客戶端進行身份驗證以進行訪問。 有關配置客戶端以進行身份​​驗證設置身份驗證端點的詳細信息,請參見http://pusher.com/docs/authenticating_users

您應該為Pusher認證Route::post('pusher/auth', 'ApiController@pusherAuth');設置路由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
        });

我希望這有幫助。

干杯!

更改

Pusher.channel_auth_endpoint = "/pusher.php";

對於:

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

我不是laravel的專家,但我猜你已經使用了get請求來檢索數據(套接字ID和通道名稱),而它是從推送服務器到服務器端點的發布請求。 使用post檢索數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM