douyouzheng2209 2013-07-17 19:40
浏览 34
已采纳

使用Javascript在同一页面中收听2个Pusher应用程序

I have been using pusher for quite a few months with success. I won't go into the details of the "push" part of the solution because that works already. My issue is with the listener side when I try to listen to a second app. Please note that I said a second app not a second channel on the same app.

Here is what I have that has worked well for at least 6 months and continues to work well until I try to add a second version of this on the same HTML/PHP page in the HEAD section.

I have changed the keys an certain info for obvious reasons.

How can I add a second copy of this pointing to a second app within Pusher?

My concern is that I will have issues if there are things like identical variables such as channel. I have tried renaming the channel to channel 2 and pusher to pusher 2 but then it quits working..

<!-- Start Pusher Code -->
  <script src="https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js" type="text/javascript"></script>
  <script type="text/javascript">

    var pusher = new Pusher('0000000000');
    var channel = pusher.subscribe('appname');

    channel.bind('channelname', function(data) {

    MyURL = 'http://www.google.com';

    newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');

    });
  </script>
  <!-- End Pusher Code -->
  • 写回答

1条回答 默认 最新

  • dongpan8928 2013-07-17 20:26
    关注

    You can use a self-executing closure:

    ( function() {
      // some code
    } )();
    

    to ensure that the variables don't clash. Also, remember to declare variables using var or they leak to the global scope.

    If possible, I would include the Pusher <script> tag just once.

    <script src="https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js"></script>
    <!-- Start Pusher Code -->
    <script>
    ( function() {
      var pusher = new Pusher('app_key_1');
      var channel = pusher.subscribe('appname');
    
      channel.bind('channelname', function(data) {
    
        var MyURL = 'http://www.google.com';
    
        var newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
    
      });
    } )();
    </script>
    <!-- End Pusher Code -->
    
    <!-- Start Pusher Code -->
    <script>
    ( function() {
      var pusher = new Pusher('app_key_2');
      var channel = pusher.subscribe('appname');
    
      channel.bind('channelname', function(data) {
    
        var MyURL = 'http://www.google.com';
    
        var newwindow = window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
    
      });
    } )();
    </script>
    <!-- End Pusher Code -->
    

    I've used two different application keys to confirm that you are connecting to two different applications.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!