我正在和拉威尔一起开发实时应用程序。因为我不想使用pusher,所以我尝试使用laravelechoserver、redis和socket.io使websockets工作。但是,这个堆栈有问题。客户端(浏览器)似乎无法从事件接收数据。我也使用intertia和vue作为前端。另外值得注意的是,我使用laragon作为本地开发环境。
我的 .env
,有些省略了:
APP_URL=http://malbe.test
BROADCAST_DRIVER=redis
CACHE_DRIVER=file
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120
MIX_APP_URL="${APP_URL}"
我的 laravel-echo-server.json
:
{
"authHost": "http://malbe.test",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "omitted",
"key": "omitted"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "http://malbe.test:80",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
我的redis配置开启 database.php
:注意:我使用 predis
因为我好像不能 phpredis
即使我在php扩展上安装了dll也可以工作。
'redis' => [
// 'client' => env('REDIS_CLIENT', 'phpredis'),
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
// 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
'prefix' => "",
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
我的拉威尔事件:
class SampleEvent implements ShouldBroadcast {
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct()
{
//
}
public function broadcastOn() {
return new Channel("my-channel");
}
public function broadcastWith() {
return ["data"=>"Hello"];
}
}
我的回音初始化在 bootstrap.js
:
import Echo from "laravel-echo";
window.io = require("socket.io-client");
if (typeof window.io !== "undefined") {
window.Echo = new Echo({
broadcaster: "socket.io",
host: process.env.MIX_APP_URL + ":6001"
});
}
在订阅echo服务器的vue页面上
export default {
props: ["foo"],
mounted: function () {
window.Echo.channel("my-channel").listen("SampleEvent", (d) => console.log(d));
}
};
运行以下3个命令时: laravel-echo-server start
npm run watch php artisan queue:work
我尝试使用tinker和get请求广播一个事件。
Route::get("/broadcast", function () {
event(new \App\Events\SampleEvent);
// also tried
// broadcast(new \App\Events\SampleEvent);
});
队列工作者处理事件;
[2020-12-24 11:07:35][n9FKq0n6C6QOKa2f1JBzWcCqgYqLiDvU] Processing: App\Events\SampleEvent
[2020-12-24 11:07:35][n9FKq0n6C6QOKa2f1JBzWcCqgYqLiDvU] Processed: App\Events\SampleEvent
以及 laravel-echo-server
每当一个事件被广播时输出这个;
L A R A V E L E C H O S E R V E R
version 1.6.2
⚠ Starting server in DEV mode...
✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
Channel: my-channel
Event: App\Events\SampleEvent
客户端未显示任何错误。下面是开发人员控制台上的“网络”选项卡:
浏览器网络选项卡
当我关上门 laravel-echo-server
当客户端仍处于打开状态时,此错误每隔一秒发生一次:
错误连接被拒绝
抱歉,问题太长了,我必须显示我的所有配置,这样我才能帮助您解决这个问题。有人能给我指出正确的解决办法吗?谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!