2017-05-05 55 views
0

我想用php和javascript实现推送器。用PHP和Javascript推送器

我已经包括与作曲家PHP推入库。

我的PHP代码是

<?php 
require_once "vendor/autoload.php"; 

$app_id = "XX"; 
$app_key = "XX"; 
$app_secret = "XX"; 
$cluster = "ap1"; 

$pusher = new Pusher($app_key, $app_secret, $app_id, array('encrypted' => true)); 

$pusher->trigger('XX-channel', 'test', 'hello world'); 

而且在JavaScript

<head> 
    <title>Pusher</title> 
    <script src="https://js.pusher.com/4.1/pusher.min.js"></script> 

</head> 
<body> 

<script type="text/javascript"> 
     const socket = new Pusher("XXX", { 
      cluster: 'ap1' 
     }); 
     const channel = socket.subscribe('XX-channel'); 

     channel.bind('test', function (data) { 
      console.log(data); 
     }); 
    </script> 
</body> 

但是当我刷新页面,我不接受任何控制台日志。

任何想法可能是错误的?

+0

您是否在PHP触发器之前加载JS? – Pogrindis

+0

另外..你是不是应该设置一个websocket服务器(可能nodeJS ??)来收听消息?你有看到什么吗? – Pogrindis

+0

我只是尝试刷新PHP页面来接收日志。 – humbleiam

回答

0
$pusher = new Pusher($app_key, $app_secret, $app_id, array('cluster'=> $cluster)); 

$pusher->trigger('XX-channel', 'test', 'hello world'); 

发现问题。

我忘了添加群集。