我正在尝试使用laravel echo服务器从lumen应用程序为我的客户端应用程序广播一个频道。在这里,我可以成功地广播一个公共频道,但当我试图广播一个私人频道时,我的laravelecho服务器抛出错误
为专用示例通道验证qewacyhqxwrbcvfmaaaa时出错错误:“uri无效”http:localhost:85/广播/认证
我已经检查了我的步骤,现在发现问题的逻辑,但这里有一些我可能是遗漏。
我现在要做的是,我已经在bootstarp/app.php文件中注册了广播服务create event and provider,并从controller调用该事件,然后从lumen app调用init laravel echo server,并为客户端app调用laravel echo+socket.io客户端。
//laravel-echo-server.json文件
{
"authHost": "http:localhost:85",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "localhost",
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
//客户端
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
auth: {
headers: {
Authorization: `Bearer ${token}`
}
}
})
// window.Echo.channel('example-channel')
window.Echo.private('example-channel')
.listen('NewMessageEvent', (e) => {
console.log('hello', e)
}).error(err => console.log('error', err))
//引导程序/app.php
$app->register(Illuminate\Broadcasting\BroadcastServiceProvider::class);
$app->register(Illuminate\Redis\RedisServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
//网站.php
$router->group(['middleware' => ['jwt.auth'], 'prefix' => 'api'], function () use ($router) {
Broadcast::channel('example-channel', function () {
Log('chennal called');
return true;
});
});
//新消息事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class NewMessageEvent extends Event implements ShouldBroadcast
{
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
//
$this->message = $message;
// $this->channel = $channel;
}
public function broadcastOn()
{
// return new Channel('example-channel');
return new PrivateChannel('example-channel');
}
public function broadcastWith()
{
$data = $this->message;
return ["data" => $data];
}
}
//类newmessagelistener
<?PHP
namespace App\Listeners;
use App\Events\NewMessageEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class NewMessageListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NewMessageEvent $event
* @return void
*/
public function handle(NewMessageEvent $event)
{
//
}
}
//触发事件
event(new \App\Events\NewMessageEvent('this is the message from the event catch it if you can'));
暂无答案!
目前还没有任何答案,快来回答吧!