【发布时间】:2020-08-26 17:41:43
【问题描述】:
第一次尝试使用laravel 和socket-io 我正在尝试向管理员发送非常简单的通知。 到目前为止,我的事件正在触发,但我在接收事件通知方面需要帮助。
逻辑
这是非常基础的,因为我想了解这个过程。
- 用户打开页面
Add Product - 管理员收到
user X在App Product页面中的通知。
到目前为止
到目前为止,我可以触发事件并获取用户数据(Add Product 页面中的用户)
需要帮助
我需要帮助来了解管理员接收通知的方式。
代码
组件脚本
created() {
let user = JSON.parse(localStorage.getItem("user"))
this.listenForBroadcast(user);
},
methods: {
listenForBroadcast(user) {
Echo.join('userInAddProduct')
.here((Loggeduser) => {
console.log('My user data', Loggeduser);
});
}
}
以上代码的结果
My user data [{…}]
0:
id: 1
name: "Test User"
photo: "User-1588137335.png"
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get id: ƒ reactiveGetter()
set id: ƒ reactiveSetter(newVal)
get name: ƒ reactiveGetter()
set name: ƒ reactiveSetter(newVal)
get photo: ƒ reactiveGetter()
set photo: ƒ reactiveSetter(newVal)
__proto__: Object
length: 1
__proto__: Array(0)
渠道路线
Broadcast::channel('userInAddProduct', function ($user) {
return [
'id' => $user->id,
'photo' => $user->photo,
'name' => $user->name
];
});
MessagePushed(事件文件)
class MessagePushed implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new PresenceChannel('userInAddProduct');
}
}
问题
如何接收有关此事件触发的通知?我想通知我的管理员用户 user x 在页面 Add Product 中?
更新
自从我发布这个问题以来,我做了一些更改,这是我最新的代码 + 问题。
bootstrap.js
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
auth: { // added authentication token (because all my events are private)
headers: {
Authorization: localStorage.getItem('access_token'),
},
},
});
Add.vue (add product component where event has to be fired)
listenForBroadcast(user) {
let ui = JSON.parse(localStorage.getItem("user"))
Echo.join('userInAddProduct')
.here((users) => {
console.log('My user data', users)
})
.joining((user) => {
this.$notify({
title: '',
message: user + 'joining',
offset: 100,
type: 'success'
});
})
.leaving((user) => {
this.$notify({
title: '',
message: user + 'is leaving new product',
offset: 100,
type: 'warning'
});
})
.whisper('typing', (e) => {
this.$notify({
title: '',
message: ui.username + 'is adding new product',
offset: 100,
type: 'success'
})
})
.listenForWhisper('typing', (e) => {
console.log(e)
this.$notify({
title: '',
message: ui.username + 'is entered add new product page.',
offset: 100,
type: 'success'
});
})
.notification((notification) => {
console.log('noitication listener: ', notification.type);
});
},
然后我制作了 4 个文件来处理事件:
- 事件 (
passing data) - 事件监听器(
process the database storage和showing notifications在线管理员) - 观察者(射击事件)
- 通知(
store data to database为管理员提供,以防事件触发时他们不在线,以便他们稍后可以看到通知)
Event file
class MessagePushed extends Event implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $product;
public function __construct(User $user, Product $product)
{
$this->user = $user;
$this->product = $product;
}
public function broadcastOn()
{
return new PresenceChannel('userInAddProduct');
}
}
Listener file
class ThingToDoAfterEventWasFired implements ShouldQueue
{
public function handle(MessagePushed $event)
{
//Log testing purpose only
$user = $event->user->username;
$product = $event->product->name;
// Real data that should be broadcasts
$user2 = $event->user;
$product2 = $event->product;
// inform all admins and authorized staffs about new product
$admins = User::role(['admin', 'staff'])->get();
foreach($admins as $admin) {
$admin->notify(new UserAddProduct($user2, $product2));
}
Log::info("Product $product was Created, by worker: $user");
}
}
Notification
class UserAddProduct extends Notification implements ShouldQueue
{
use Queueable;
protected $product;
protected $user;
public function __construct(User $user, Product $product)
{
$this->product = $product;
$this->user = $user;
}
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toDatabase($notifiable)
{
return [
'user_id' => $this->user->id,
'user_username' => $this->user->username,
'product_id' => $this->product->id,
'product_name' => $this->product->name,
];
}
public function toArray($notifiable)
{
return [
'id' => $this->id,
'read_at' => null,
'data' => [
'user_id' => $this->user->id,
'user_username' => $this->user->username,
'product_id' => $this->product->id,
'product_name' => $this->product->name,
],
];
}
}
Observer
public function created(Product $product)
{
$user = Auth::user();
event(new MessagePushed($user, $product));
}
问题
- 如何在在整个应用程序中触发事件后立即返回实时通知?目前我的代码放在
add.vue component admins get notify IF they are in same page only :/ - 如何获得多个事件的通知?假设我有另一个
event, listener, observer用于其他页面操作,我希望管理员在整个应用中同时收到product event和other event的通知。
谢谢
【问题讨论】:
-
你在客户端使用 socket.io:socket.io/docs 吗?如果是,您可以简单地使用 socket.on('event_fire_name', function(){ /* */ });在管理面板中接收通知
-
@AhmedRebai 是的,我在客户端使用套接字,您有任何示例代码可以分享作为答案吗?
-
@maforis 看看官方文档socket.io/docs/client-api,它会指导你
-
....没有人?! ://
-
@Shizzen83 非常感谢你,迫不及待想看:)