Pusher: How to bind to 100s of events?

霸气de小男生 提交于 2019-12-08 03:31:25

问题


The push library works as below

var channel = pusher.subscribe('test_channel');
channel.bind('my_event', function(data) {
  alert(data.message);
});

However: Would I be able to do this?

var channel = pusher.subscribe('test_channel');
channel.bind(['my_event1', 'my_event2'....'my_event100'], function(data) {
  alert(data.message);
});

In my use case, I have one channel and there are many different events and each client might want to simulantaneously subscribe to 100s of events.


回答1:


The signature for the channel.bind function is String channelName, Function callback (pusher-js source). You can't pass in an Array of channels`.

If you want the same function to be called then you'll need to pass a reference to the function and call bind multiple times:

var channel = pusher.subscribe('test_channel');
var callback = function(data) {
  alert(data.message);
};

var eventName;
for( var i = 0; i < 100; ++i ) {
  eventName = 'my_event' + ( i + 1 );
  channel.bind( eventName, callback );
}

The single-threaded nature of JS will equate to these event binding happening simultaneously.

You could of course create your own helper function to allow bind( Array eventNames, Function callback ).



来源:https://stackoverflow.com/questions/20892778/pusher-how-to-bind-to-100s-of-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!