[英]How to trigger pusher event from client side in vue js?
I am using ionic with vue js mobile app.我正在将 ionic 与 vue js 移动应用程序一起使用。 I want to trigger event from client side when location on mobile user is changed.
当移动用户的位置发生变化时,我想从客户端触发事件。
methods: {
getLocation: function () {
const geolocation = new GeolocationService.Geolocation();
let watch = geolocation.watchPosition();
watch.subscribe((data) => {
console.log('data',data)
//Trigger pusher event here
});
},
},
And then I want to listen that event on my web app with help of jquery.然后我想在 jquery 的帮助下在我的 web 应用程序上收听该事件。 But I don't know how I can trigger pusher event from client side
但我不知道如何从客户端触发推送事件
I am using this pusher package我正在使用这个推杆 package
https://www.npmjs.com/package/vue-pusher https://www.npmjs.com/package/vue-pusher
The Pusher documentation about triggering client events has every information for your case.关于触发客户端事件的 Pusher 文档包含您案例的所有信息。
Starting with:从...开始:
Not all traffic needs to go via your conventional web server when using Channels.
使用 Channels 时,并非所有流量都需要通过传统的 web 服务器到达 go。 Some actions may not need validation or persistence and can go directly via the socket to all the other clients connected to the channel.
某些操作可能不需要验证或持久性,并且可以 go 直接通过套接字连接到通道的所有其他客户端。
Some other important things to highlight:需要强调的其他一些重要事项:
Using a wrapper like vue-pusher is totally optional, you can always instantiate a new Pusher
and use the client directly.使用像vue-pusher这样的包装器是完全可选的,你总是可以实例化一个
new Pusher
并直接使用客户端。 But if you really like the wrapper, your code could be something like that:但如果你真的喜欢包装器,你的代码可能是这样的:
export default { data() { return { channel: null }; }, created() { this.channel = this.$pusher.subscribe('private-positions'); }, methods: { getLocation() { const geolocation = new GeolocationService.Geolocation(); const watchPosition = geolocation.watchPosition(); watchPosition.subscribe((data) => { this.channel.trigger("client-positionChanged", { data }); }); } } }
Of course the Pusher authentication part should be considered too.当然也应该考虑 Pusher 认证部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.