Pusher与Laravel 5认证

jac*_*013 4 php chat websocket laravel pusher

我正在使用Laravel 5中的实时聊天制作应用程序,我正在关注本教程,https://github.com/dazzz1er/confer/tree/master我已经关注了所有这些但是我的错误网络控制台:

在此输入图像描述

似乎它正在我的网址http://localhost/joene_/public/index.php/auth上进行ajax调用,因为我没有处理该请求的路由,它说404.我不知道是否应该为它做一条路线,但我会在那里编码?我不知道.该教程甚至没有提到它.

谢谢

Mys*_*yos 8

每当您致电时Auth::check(),Laravel将通过检查其会话信息来验证用户是否已通过身份验证.

Pusher怎么样?他们如何知道哪些用户当前登录了您的laravel应用程序?

答案在于ajax调用http://localhost/joene_/public/index.php/auth.

通过调用上述URL,您的laravel安装将让您的Pusher应用程序与您的用户的laravel会话链接.

让我们深入研究一些代码:

1)Pusher Auth控制器

class PusherController extends Controller {

    //accessed through '/pusher/'
    //setup your routes.php accordingly

    public function __construct() {
        parent::__construct();
        //Let's register our pusher application with the server.
        //I have used my own config files. The config keys are self-explanatory.
        //You have received these config values from pusher itself, when you signed up for their service.
        $this->pusher = new Pusher(\Config::get('pusher.app_key'), \Config::get('pusher.app_secret'), \Config::get('pusher.app_id'));
    }

    /**
     * Authenticates logged-in user in the Pusher JS app
     * For presence channels
     */
    public function postAuth()
    {
        //We see if the user is logged in our laravel application.
        if(\Auth::check())
        {
            //Fetch User Object
            $user =  \Auth::user();
            //Presence Channel information. Usually contains personal user information.
            //See: https://pusher.com/docs/client_api_guide/client_presence_channels
            $presence_data = array('name' => $user->first_name." ".$user->last_name);
            //Registers users' presence channel.
            echo $this->pusher->presence_auth(Input::get('channel_name'), Input::get('socket_id'), $user->id, $presence_data);       
        }
        else
        {
            return Response::make('Forbidden',403);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2)JS与Pusher一起使用

//replace 'xxx' below with your app key
var pusher = new Pusher('xxx',{authEndpoint : '/pusher/auth'});
var presenceChannelCurrent = pusher.subscribe('presence-myapp');
presenceChannelCurrent.bind('pusher:subscription_succeeded', function() {
    alert(presenceChannelCurrent.members.me.info.name+' has successfully subscribed to the Pusher Presence Channel - My App');
});
Run Code Online (Sandbox Code Playgroud)

希望它能帮到你.