推送者 - 私有频道订阅

8

我有一个订阅私有频道的代码,当我尝试进行订阅时,出现了下面的信息:

Pusher: 无法从您的Web应用程序获取身份验证信息: 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 

我找到了问题所在。我需要在我的服务器上创建一个登录以获取推送功能的AUTH。已修复!!! - user2842722
你能否再解释一下答案?如果你还记得的话(我知道那是一段时间以前)。 - hipkiss
4个回答

3
你应该为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";

0

我不是 Laravel 专家,但我猜你使用了 GET 请求来检索数据(套接字 ID 和频道名称),但实际上 Pusher 服务器向您的服务器端点发送的是 POST 请求。请使用 POST 请求来检索数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接