8

实时通信 | pusher 客户端事件(五) - Tinywan

 2 years ago
source link: https://www.cnblogs.com/tinywan/p/15854186.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

实时通信 | pusher 客户端事件(五)

在整个系列中,我们严格地研究了服务器事件-源于服务器的事件-但我们也能够触发来自客户端的事件。 这些称为客户端事件,它们提供的服务与服务器事件不同,因为某些操作可能不需要验证或持久性。

客户端事件具有几个限制,您的应用程序必须遵守这些限制才能使用它们。 最值得注意的是:

  • 必须通过“渠道”应用程序的仪表板启用客户端事件。
  • 客户事件只能在私人和在线渠道上发布。
  • 它们不会传递给发起事件的客户端。
  • 每个客户端每秒不得超过十条消息。

大多数绑定和触发行为都附加到客户端订阅的频道,尽管也可以绑定到频道连接上的所有事件,而不管频道如何。

事件可以直接绑定到通道上,这意味着只有在该特定通道上发送事件时,您才会收到事件。

var pusher = new Pusher("APP_KEY");
var channel = pusher.subscribe("my-channel");
channel.bind("my-event", (data) => {

});

(1)订阅频道

(2)频道绑定事件

 在客户端绑定

无论事件广播到哪个频道,您也可以绑定到事件。通过使用 pusher.bind() 方法而不是 channel.bind(),您可以在当前订阅的所有频道上侦听事件。

以下是一个应用程序示例,该应用程序绑定到我们当前订阅的所有频道的新评论事件:

var pusher = new Pusher("APP_KEY");
var channel1 = pusher.subscribe("my-channel-1");
var channel2 = pusher.subscribe("my-channel-2");
var channel3 = pusher.subscribe("my-channel-3");
var eventName = "new-comment";
var callback = (data) => {
  // add comment into page
};
// listen for 'new-comment' event on channel 1, 2 and 3
pusher.bind(eventName, callback);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK