أنا أريد أن احول laravel public channel ل private channel لكي تعمل مع pusher بدون laravel echo
و هذا الكود في ملف events/newNotification
class NewNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
protected $user_id;
protected $comment;
protected $user_name;
protected $post_id;
protected $data;
protected $time;
protected $post_user_id;
public function __construct($data = [])
{
$this->user_id = $data['user_id'];
$this->user_name = $data['user_name'];
$this->comment = $data['comment'];
$this->post_id = $data['post_id'];
$this->post_user_id = $data['post_user_id'];
$this->date = date("Y-m-d", strtotime(Carbon::now()));
$this->time = date("h:i A", strtotime(Carbon::now()));
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('new-notification.'.$this->post_user_id);
}
}
وهذا الكود في ملف blade
Pusher.logToConsole = true;
var pusher = new Pusher("4fb4fa908d87ec86abf9", {
cluster: 'eu'
});
var notificationsWrapper = $('.dropdown-notifications');
var notificationsToggle = notificationsWrapper.find('a[data-toggle]');
var notificationsCountElem = notificationsToggle.find('span[data-count]');
var notificationsCount = parseInt(notificationsCountElem.data('count'));
var notifications = notificationsWrapper.find('li.scrollable-container');
// Subscribe to the channel we specified in our Laravel Event
var channel = pusher.subscribe('private-new-notification.'+1);
// Bind a function to a Event (the full Laravel class)
channel.bind('App\\Events\\NewNotification', function (data) {
var existingNotifications = notifications.html();
var newNotificationHtml = `<ahref="`+data.user_id+`"><divclass="media-body"><h6class="media-heading text-right">` + data.user_name + `</h6><pclass="notification-text font-small-3 text-muted text-right">` + data.comment + `</p><smallstyle="direction: ltr;"><pclass="media-meta text-muted text-right"style="direction: ltr;">` + data.date + data.time + `</p></small></div></div></a>`;
notifications.html(newNotificationHtml + existingNotifications);
notificationsCount += 1;
notificationsCountElem.attr('data-count', notificationsCount);
notificationsWrapper.find('.notif-count').text(notificationsCount);
notificationsWrapper.show();
});
</script>
السؤال
Ziyad Hasan
أنا أريد أن احول laravel public channel ل private channel لكي تعمل مع pusher بدون laravel echo
و هذا الكود في ملف events/newNotification
class NewNotification implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels; /** * Create a new event instance. * * @return void */ protected $user_id; protected $comment; protected $user_name; protected $post_id; protected $data; protected $time; protected $post_user_id; public function __construct($data = []) { $this->user_id = $data['user_id']; $this->user_name = $data['user_name']; $this->comment = $data['comment']; $this->post_id = $data['post_id']; $this->post_user_id = $data['post_user_id']; $this->date = date("Y-m-d", strtotime(Carbon::now())); $this->time = date("h:i A", strtotime(Carbon::now())); } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('new-notification.'.$this->post_user_id); } }
وهذا الكود في ملف blade
3 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.