我使用了带有echo和pusher js的laravel websockets。这是我的代码:
bootstrap.js:
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Pusher.Runtime.createXHR = function () {
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
return xhr;
};
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: false,
wsHost: window.location.hostname,
wsPort: 6001,
wssPort: 6001,
});broadcasting.php:
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => false,
'host' => '127.0.0.1',
'port' => 6001,
'scheme' => 'https',
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],websockets.php
'ssl' => [
/*
* Path to local certificate file on filesystem. It must be a PEM encoded file which
* contains your certificate and private key. It can optionally contain the
* certificate chain of issuers. The private key also may be contained
* in a separate file specified by local_pk.
*/
'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
/*
* Path to local private key file on filesystem in case of separate files for
* certificate (local_cert) and private key.
*/
'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
/*
* Passphrase for your local_cert file.
*/
'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
],在本地主机上运行时,一切都正常工作。但是当我试图在服务器上运行它时,我在echo listener页面控制台上得到了这个错误信息:(ubuntu server + ssl)
WebSocket connection to 'wss://example.com/app/ZLZVrjyIVjiXlUCF?protocol=7&client=js&version=5.0.0&flash=false' failed: Error during WebSocket handshake: Unexpected response code: 404
OPTIONS https://sockjs-mt1.pusher.com/pusher/app/ZLZVrjyIVjiXlUCF/963/qo637plk/xhr_streaming?protocol=7&client=js&version=5.0.0&t=1569413857094&n=1 404 (Not Found)
Access to XMLHttpRequest at 'https://sockjs-mt1.pusher.com/pusher/app/ZLZVrjyIVjiXlUCF/963/qo637plk/xhr_streaming?protocol=7&client=js&version=5.0.0&t=1569413857094&n=1' from origin 'https://example.com' 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.每3秒重复一次!你有什么办法解决这个问题吗?!:(
发布于 2021-02-11 15:11:18
我通过将pusher-js版本从6.0.0改为5.1.1解决了这个问题。在(Laravel vuejs项目)
发布于 2021-05-28 03:04:11
在我的例子中,我向config/cors.php添加了以下内容
'paths' => ['api/v1/*', 'stats/*', 'broadcasting/auth'],并确定了authEndpoint,因为pusher正在另一台服务器上运行
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: JSON.parse(process.env.MIX_PUSHER_APP_FORCE_TLS),
wsHost: process.env.MIX_PUSHER_WS_HOST,
wsPort: 6001,
enabledTransports: ['ws', 'wss'],
authEndpoint: 'http://127.0.0.1:8000/broadcasting/auth',
auth: {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('MY-AUTH-TOKEN')
}
}https://stackoverflow.com/questions/58098617
复制相似问题