我使用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 Echo选项中的代码
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!';
});
如果删除wsHost
和wsPort
,则没有错误,但什么也没有发生,服务器没有响应。
2条答案
按热度按时间kgqe7b3p1#
这个问题可能已经解决了,但是对于那些仍然无法让前端监听事件的人来说,对我来说有效的是将pusher-js恢复到5.1.1版本,在repo上找到了这个问题的答案:https://github.com/beyondcode/laravel-websockets/issues/365
daupos2t2#
我也遇到了同样的问题(Laravel和Vuejs),它在localhost中工作,但当我托管到vps时,CORS问题在控制台中显示。我把pushers-js的版本从6.0.0改成了5.1.1,这对我很有效。