我使用 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...',
},
},
});
广播服务提供者.php
Broadcast::routes(['middleware' => 'auth:api']);
频道.php
Broadcast::channel('chat', function () {
return 'Hello!';
});
如果删除wsHost
然后wsPort
没有错误,但没有任何反应,则服务器没有响应。