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>
但是當我刷新頁面,我不接受任何控制檯日誌。
任何想法可能是錯誤的?
您是否在PHP觸發器之前加載JS? – Pogrindis
另外..你是不是應該設置一個websocket服務器(可能nodeJS ??)來收聽消息?你有看到什麼嗎? – Pogrindis
我只是嘗試刷新PHP頁面來接收日誌。 – humbleiam