gpt4 book ai didi

ubuntu 服务器上的 laravel websockets 和 pusher-js CROS 问题

转载 作者:行者123 更新时间:2023-12-03 11:50:25 24 4
gpt4 key购买 nike

我用了laravel websockets带有回声和推送器 js。这是我的代码:
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,
});

广播.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 监听器页面控制台上收到此错误消息:(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 秒重复一次!你有解决这个问题的想法吗?! :(

最佳答案

在我的例子中,我向 config/cors.php 添加了以下内容


'paths' => ['api/v1/*', 'stats/*', 'broadcasting/auth'],


并确定了 authEndpoint,因为推送器在另一台服务器上运行

  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')
}
}

关于ubuntu 服务器上的 laravel websockets 和 pusher-js CROS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com