Laravel Echo与Pusher和Vue

时间:2020-03-01 20:18:06

标签: laravel vue.js laravel-echo pusher-js laravel-websockets

我使用Laravel作为服务器部件,使用Vue Cli作为客户端。 我也用https://docs.beyondco.de/laravel-websockets/ 但是,当我尝试将vue客户端连接到laravel websockets时,控制台日志中出现错误。

WebSocket connection to 'ws://http//websockets.test:6001/app/myKey?protocol=7&client=js&version=5.1.1&flash=false' failed: Connection closed before receiving a handshake response

WebSocket connection to 'wss://http//websockets.test:443/app/myKey?protocol=7&client=js&version=5.1.1&flash=false' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

Access to XMLHttpRequest at 'http://sockjs-mt1.pusher.com/pusher/app/myKey/659/6nenb2dd/xhr_streaming?protocol=7&client=js&version=5.1.1&t=1583092642805&n=1' from origin 'http://app.vuesocks.test:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这是我在Vue Cli index.js回显选项中的代码

window.Echo = new Echo({
  broadcaster: 'pusher',
  key: 'myKey',
  cluster: 'mt1',
  authEndpoint: 'http://websockets.test/broadcasting/auth',
  wsHost: 'http://websockets.test',
  wsPort: 6001,
  disableStats: true,
  auth: {
    headers: {
      Authorization: 'Bearer myToken...',
    },
  },
});

BroadcastServiceProvider.php

Broadcast::routes(['middleware' => 'auth:api']);

channel.php

Broadcast::channel('chat', function () {
    return 'Hello!';
});

如果删除wsHostwsPort,则没有错误,但是什么也没有发生,服务器没有响应。

2 个答案:

答案 0 :(得分:1)

这可能已经解决了,但是对于仍然无法让前端监听事件的任何人,对我有用的是将pusher-js恢复到5.1.1版,在存储库中找到了有关此问题的答案:https://github.com/beyondcode/laravel-websockets/issues/365

答案 1 :(得分:0)

我遇到了同样的问题(Laravel 和 Vuejs),它在 localhost 中工作,但是当我托管 vps 时,控制台中显示了 CORS 问题。 我将 pushers-js 版本从 6.0.0 更改为 5.1.1,这对我有用。