https://medium.com/@dennissmink/laravel-echo-server-private-channels-267a9e57bae9
laravel回声有问题
window.Echo.private(`chat.${chatId}`)
.listen('PushMessageEvent', (data) => {
console.log('echoPushMessage133', data);
dispatch({
type: MESSAGES_ECHO_PUSH_MESSAGE,
data
});
})
PushMessageEvent
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class PushMessageEvent implements ShouldBroadcast
{
public $chatMessage;
public $chatId;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($chatMessage, $chatId)
{
$this->chatMessage = $chatMessage;
$this->chatId = $chatId;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('chat.'.$this->chatId);
}
}
routes \ channels.php
Broadcast::channel('chat.{chatId}', function ($user, $chatId) {
\Illuminate\Support\Facades\Log::info('1111');
return true;
return ChatMessage::where([
['user_id', $user->id],
['chat_id', $chatId]
])->count() > 0;
});
在开放频道下工作正常,但在私人更改下会出现错误
⚠ [19:39:47] - wVU2a2oSqfPYcBOAAAAG could not be authenticated to private-chat.3
{
"message": "The POST method is not supported for this route. Supported methods: GET, HEAD.",
Client can not be authenticated, got HTTP status 405
和
Channel: private-chat.3
Event: App\Events\PushMessageEvent
但是js中的事件不起作用