当前位置: 首页 > 知识库问答 >
问题:

laravel echo此路由不支持POST方法

邵麒
2023-03-14

所以我试图连接到一个私人频道使用Laravel回声。我能够连接到一个公共通道,但我得到的问题验证(用正确的承载令牌登录)私人通道-得到以下错误从laravel-echo-server start

L A R A V E L  E C H O  S E R V E R

version 1.6.2

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

[10:58:39 PM] - Preparing authentication request to: https://example.co.uk
[10:58:39 PM] - Sending auth request to: https://example.co.uk/api/broadcasting/auth/

⚠ [10:58:39 PM] - K1cJaXs7jWMBNpF9AAAA could not be authenticated to private-sonect_database_user.2
{"errors":{"message":"The POST method is not supported for this route. Supported methods: GET, HEAD."}}
Client can not be authenticated, got HTTP status 401

我被不支持POST方法的错误弄糊涂了,因为所有的POST、PUT、PATCH和DELETE都在我的Laravel项目中工作。此外,我能够击中https://example.co.uk/api/broadcasting/auth/使用Postman与POST请求。

下面是一个php artisan route:list的示例

| Domain | Method        | URI                                           | Name                              | Action                                                                    | Middleware   |
|        | GET|POST|HEAD | api/broadcasting/auth                         |                                   | Illuminate\Broadcasting\BroadcastController@authenticate                  | auth:api     |

游戏事件。php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PlayerEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @param User $user
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('user.' . $this->user->id);
    }

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'PlayerEvent';
    }

    /**
     * The event's broadcast name.
     *
     * @return array
     */
    public function broadcastWith()
    {
        return ['message' => 'Hello world'];
    }

}

BroadcaseServiceProvider。php

    public function boot()
    {
        Broadcast::routes(["prefix" => "api", "middleware" => "auth:api"]);

        require base_path('routes/channels.php');
    }

配置/app.php


        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

路线/频道。php


Broadcast::channel('user.{user}', function ($user) {
    return true;
});

laravel-echo-server.json

{
    "authHost": "https://example.co.uk",
    "authEndpoint": "/api/broadcasting/auth/",
    "clients": [
        {
            "appId": "4cb2c72910d85007",
            "key": "d3af62258887434b9900b89a937cb7f7"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        },
        "publishPresence": true
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "https",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "/var/www/example.co.uk/letsencrypt/live/example.co.uk/fullchain.pem",
    "sslKeyPath": "/var/www/example.co.uk/letsencrypt/live/example.co.uk/privkey.pem",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "http://localhost:4200/",
        "allowMethods": "GET, POST, PUT, PATCH, DELETE",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

我在前端使用angular

回响服务ts

import {Injectable} from "@angular/core";
import {environment} from "../../../environments/environment";
import Echo from "laravel-echo";
import {UserModel} from "../models/user.model";
import * as io from "socket.io-client";

@Injectable({
    providedIn: 'root'
})
export class EchoService {

    public echo: Echo;

    constructor() {
        window['io'] = io;
        this.echo = window['Echo'] = new Echo({
            broadcaster: 'socket.io',
            host: environment.websocketUrl, // https://example.co.uk:6001
            auth: {
                headers: {
                    Authorization: `Bearer ${localStorage.getItem('token')}`
                }
            }
        });
    }

    /** privateChannel('user', 'PlayerEvent', this.user); Called from elsewhere */
    privateChannel(channelName: string, eventName: string, user: UserModel) {
        console.log('privateChannel');
        this.echo.private( environment.broadcastChannelPrefix + channelName + '.' + user.id)
            .listen('.' + eventName, data => {
                console.log(data);
            });
    }
}

拉雷维尔:7.12。0
拉维回声:^1.8。0
插座。io客户端:^2.3。0
角度:9.1。9

共有1个答案

孟承嗣
2023-03-14

所以我想通了。有点明显当我意识到...这与我的开发环境有关——我有一个运行的虚拟机,我在那里编辑了我的/etc/host/文件来指向example.co.uk。由于laravel-echo从服务器验证用户,example.co.uk现在指向我的实时服务器,该路由没有正确设置。

将虚拟机上的/etc/host文件更改为指向:

192.168.33.10     example.co.uk

修正了这个问题

 类似资料:
  • 问题内容: 当我使用下面的代码时(以下),我得到了错误…引起故障的行(显然)是 编辑:我按照建议添加了,但这并没有改变错误。 编辑:EditObject servlet看起来像这样 另一个编辑:所以基本上我不能做我正在做的事情。我需要的是,用户提交一个发布请求,然后将他/她转回到使用Get方法而不是Post的servlet。如何在不出现错误的情况下进行此引荐?提前致谢。 问题答案: (很抱歉,我之

  • 问题内容: 我无法使页面正常工作。我有要发布的form方法和servlet实现。但是,它不断向我显示我不支持 POST 方法。 我只是想做一个简单的网站,并将值插入到我的MySQL DB中。 静态页面: Servlet: web.xml: 编辑:删除doGet(request,response); 问题答案: 这是因为您在打电话时并未实际实现。这是doGet()的默认实现,会引发错误,指出该方法不

  • 我正在编写一个控制器来处理来自AJAX的帖子。我一直收到一个错误,那篇文章不受支持。我以前在尝试创建后控制器方法时从未遇到过以下错误,我希望有人能发现我在哪里搞砸了。 这是我为控制控制器中的帖子而编写的方法: 使用JQuery 1.10,这是请求它的Ajax调用: 我知道POST地址是正确的,因为将它转换成GET请求就可以了。此外,我还编写了其他POST请求,它们在同一个控制器类中也能正常工作。任

  • login.jsp 用户列表.jsp AppController.java 有2页:login.jsp-起始页,其中包括与登录和密码填充的形式-userlist.jsp结果列表“显示数据库中所有用户持久化”..首先显示登录页面,当我单击提交按钮时,我得到了这个错误:org . spring framework . web . servlet . pagenotfound-不支持请求方法“POST”

  • 下面是我的方法 当调用GET request时,它的工作很好,但是当我们调用POST request和request payload checkout_token=xxxx时,我得到以下错误 编辑 甚至我尝试删除所有参数,但仍然没有运气到目前为止。

  • 我刚刚开始学习Java Spring Boot for REST API开发。在下面的代码中,GET方法可以正常工作,但POST不能。 在Postman应用程序中使用as 错误 在日志中我可以看到, maven的Java和springboot版本,