简体   繁体   English

如何在vue js中从客户端触发推送事件?

[英]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:需要强调的其他一些重要事项:

  • Client events must be enabled for the application inside Pusher dashboard.必须为 Pusher 仪表板内的应用程序启用客户端事件。
  • The user must be subscribed to the channel that the event is being triggered on.用户必须订阅触发事件的频道。
  • Client events can only be triggered on private and presence channels because they require authentication.客户端事件只能在私有和在线通道上触发,因为它们需要身份验证。 So understanding Pusher authentication is a must.所以理解Pusher 认证是必须的。
  • Publish no more than 10 messages per second per client (connection).每个客户端(连接)每秒发布不超过 10 条消息。 So user location changing every second shouldn't be a problem.因此,每秒更改用户位置应该不是问题。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM