Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Javascript在同一页面中侦听2个推送应用程序_Php_Javascript_Twilio_Pusher - Fatal编程技术网

Php 使用Javascript在同一页面中侦听2个推送应用程序

Php 使用Javascript在同一页面中侦听2个推送应用程序,php,javascript,twilio,pusher,Php,Javascript,Twilio,Pusher,我使用pusher已经有好几个月了,并取得了成功。我不会详细讨论解决方案的“推送”部分,因为这已经起作用了。我的问题是,当我尝试收听第二个应用程序时,侦听器端的问题。请注意,我说的是第二个应用程序,而不是同一应用程序上的第二个频道 这是我已经做了至少6个月的工作,并且一直很好,直到我尝试在HEAD部分的同一HTML/PHP页面上添加第二个版本 由于明显的原因,我更改了某些信息的密钥 如何在Pusher中添加指向第二个应用程序的第二个副本 我担心的是,如果存在相同的变量(如通道),我会遇到问题。我

我使用pusher已经有好几个月了,并取得了成功。我不会详细讨论解决方案的“推送”部分,因为这已经起作用了。我的问题是,当我尝试收听第二个应用程序时,侦听器端的问题。请注意,我说的是第二个应用程序,而不是同一应用程序上的第二个频道

这是我已经做了至少6个月的工作,并且一直很好,直到我尝试在HEAD部分的同一HTML/PHP页面上添加第二个版本

由于明显的原因,我更改了某些信息的密钥

如何在Pusher中添加指向第二个应用程序的第二个副本

我担心的是,如果存在相同的变量(如通道),我会遇到问题。我曾尝试将频道重命名为频道2,将pusher重命名为pusher 2,但随后它停止工作

<!-- 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 -->

var pusher=新pusher('0000000000');
var channel=pusher.subscribe('appname');
channel.bind('channelname',函数(数据){
我的URL=http://www.google.com';
newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
});

您可以使用自动执行的闭包:

( function() {
  // some code
} )();
以确保变量不会冲突。另外,请记住使用
var
声明变量,否则它们会泄漏到全局范围

如果可能的话,我将只包含一次Pusher
标签

<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 -->

(功能(){
var推进器=新推进器(“app_键_1”);
var channel=pusher.subscribe('appname');
channel.bind('channelname',函数(数据){
var MyURL='1〕http://www.google.com';
var newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
});
} )();
(功能(){
var推进器=新推进器(“app_键_2”);
var channel=pusher.subscribe('appname');
channel.bind('channelname',函数(数据){
var MyURL='1〕http://www.google.com';
var newwindow=window.open(MyURL,data.cuid,'height=800,width=950,scrollbars=yes');
});
} )();

我使用了两个不同的应用程序密钥来确认您正在连接到两个不同的应用程序。

好的,我将试一试。我只是担心你提到的冲突。如果你给我的代码能够做到这一点,并且不会引起对“数据”的引用,从而混淆等等,那么这就是我希望实现的目标。虽然,我不得不说,我很好奇为什么我可以直接重命名“pusher”和“channel”变量。。不知怎的,这破坏了代码。顺便说一句,我不确定在两种情况下保持变量相同实际上是一个问题,但是。。。我的大脑告诉我,这可能是真的。再次感谢!
data
变量只存在于它所传递给的函数的范围内。很可能被覆盖的变量没有引起问题——最好不要这样做。原因可能是两次包含Pusher脚本标记。它可能已经覆盖了引用。我不是百分之百确定,但很高兴上述方法能解决问题。只是想进一步澄清一下。。如果我重复使用变量,我不能100%确定它是否会被破坏。我只是不想这样,当我试图改变它们时,这就破坏了一切。我从来没有真正尝试过草率行事,保持变量不变,这就是为什么我需要建议。一直在学习:-)再次感谢!