互换的青春
编辑 1我真的不确定这是否真的有效,但你可以尝试一下吗class LaravelEcho extends Echo { constructor(options) { super(options); this.setformData(); } setformData(formData = this.options.formData) { if (formData) { let path = "?" + Object.entries(formData) .map((ch) => ch.join("=")) .join("&"); this.options.authEndpoint += path; this.connector.options = this.options; // since you are using pusher if (this.connector.pusher) { this.connector.pusher.options = this.options; } // maybe also for socket.io too? else if (this.connector.socket) { this.connector.socket.options = this.options; } } }}let myEcho = new LaravelEcho({ broadcaster: "pusher", key: "somekey", wsHost: "127.0.0.1", wsPort: 6001, encrypted: false, disableStats: true, forceTLS: false, authEndpoint: "http://127.0.0.1:8000/broadcasting/auth", formData: { foo: "bar", username: "username", password: "password", },});console.log(myEcho);我知道这真的不是你想要的方式。我试图按照@Islam 在评论中所说的那样做到这一点,我真的想知道如果我们只是在创建后覆盖选项,这是否会像这样工作:)老的我正在调查这个。在这里我看到有一个headers选项如下:private _defaultOptions: any = { auth: { headers: {}, }, authEndpoint: '/broadcasting/auth', broadcaster: 'pusher', csrfToken: null, host: null, key: null, namespace: 'App.Events',};这是Connecter's default options 并且在它里面它也在这里constructor设置了一个 auth headercsrfToken所以我猜你在创建 Laravel/Echo 时可能会这样做,window.Echo = new Echo({ broadcaster: 'pusher', key: 'somekey', wsHost: '127.0.0.1', wsPort: 6001, encrypted: false, disableStats: true, forceTLS: false, authEndpoint: 'http://127.0.0.1:8000/broadcasting/auth', auth: { headers: { 'X-CSRF-TOKEN': 'your-csrf-token' 'your-header': 'with-value' } } });希望这对你有用。请务必告诉我!:)顺便说一句,我没有测试环境,所以我从未测试过它..