Laravel-Echo + Passport认证+ Node JS客户端

时间:2019-07-10 10:15:21

标签: node.js laravel laravel-echo

您好,我正在尝试设置节点客户端以连接到laravel-echo服务器。

我的身份验证存在一些问题。

查看laravel-echo服务器日志,我总是从广播/身份验证端点获得403响应。

我按照this帖子设置了laravel

带有护照和警卫的身份验证系统可以很好地与服务器路由配合使用。

laravel-echo-server.json

{
"authHost": "http://localhost",
"authEndpoint": "/itproxy/public/api/broadcasting/auth",
"clients": [
    {
        "appId": "dac0e3e9adf8b725",
        "key": "b011f815bb6ed4739af99658f48255cb"
    }
],
"database": "redis",
"databaseConfig": {
    "redis": {},
    "sqlite": {
        "databasePath": "/database/laravel-echo-server.sqlite"
    }
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
    "http": true,
    "redis": true
},
"apiOriginAllow": {
    "allowCors": true,
    "allowOrigin": "http://localhost:80",
    "allowMethods": "(get,post)",
    "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}

}

BroadCastServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes(["prefix" => "api", "middleware" => "auth:api"]);
        require base_path('routes/channels.php');
    }
}

这是客户端app.ts

// lib/app.ts
import express = require('express');

import Echo from 'laravel-echo';
const axios = require('axios');
var token = "";

//const app: express.Application = express();

let io = require('socket.io-client');
let echo: Echo;

login();
startServer();
sendData();

function login() {
    axios.post(' http://3.16.169.253/itproxy/public/login', {
    username: '*****',
    password: '**',
    plant_code: '****'
  })
    .then((res: any) => {
      if (res.status == 200) {
        token = res.data.success.token;
        startServer();
      }

    })
    .catch((error: any) => {
      console.log("UNAUTHORIZED")
      console.error(error)
    })

}

function startServer() {
   echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://3.16.169.253:6001',
    client: io,
    auth: { headers: { Authorization: "Bearer " + token } }
  });


  echo.private('pippo').listen('drumdata', (e: any) => {
    console.log("DATA RECIVED");
    console.log(e);
  })


}



async function sendData() {
  await sleep(6000)

  for (let i = 0; i < 10; i++) {
    await sleep(2000)
   //echo.connector.options.auth.headers['X-Socket-ID'] = 
echo.connector.socket.id
    // echo.connector.socket.emit("drumdata", "public", 
   {"eventData":"myData"}) ;
  }
}


function sleep(ms: number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

laravel-echo-server日志

[9:46:14 AM] - Preparing authentication request to: http://localhost
[9:46:14 AM] - Sending auth request to: 
http://localhost/itproxy/public/api/broadcasting/auth

2019-07-10T09:46:14.063Z engine intercepting request for path "/socket.io/"
2019-07-10T09:46:14.063Z engine handling "GET" http request "/socket.io/ EIO=3&transport=polling&t=MlRHgzl.0&b64=1&sid=i1pzGGkl9lr8lhhEAAAA"
2019-07-10T09:46:14.063Z engine setting new request for existing client
2019-07-10T09:46:14.063Z engine:polling setting request
2019-07-10T09:46:14.407Z engine:ws received "2probe"
2019-07-10T09:46:14.407Z engine:ws writing "3probe"
2019-07-10T09:46:14.517Z engine:socket writing a noop packet to polling for fast upgrade
2019-07-10T09:46:14.517Z engine:polling writing "1:6"
⚠ [9:46:14 AM] - i1pzGGkl9lr8lhhEAAAA could not be authenticated to 
private-pippo
{
    "message": "",
    "exception": 
"Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException",
    "file": "C:\\xampp\\htdocs\\itproxy\\vendor\\laravel\\framework\\src\\Illuminate\\Bro 
   adcasting\\Broadcasters\\Broadcaster.php",
     "line": 81,
       "trace": [
         {
             "file": "C:\\xampp\\htdocs\\itproxy\\vendor\\laravel\\framework\\src\\Illuminate\\Bro 
   adcasting\\Broadcasters\\RedisBroadcaster.php",
           "line": 58,
            "function": "verifyUserCanAccessChannel",
           "class": "Illumnate\\Broadcasting\\Broadcasters\\Broadcaster",
        "type": "->"
     },

希望对在哪里看以及如何测试广播/身份验证服务是否正常工作提出一些建议。

感谢费德里科。

0 个答案:

没有答案