【发布时间】:2015-10-24 19:07:08
【问题描述】:
我正在使用 Pusher 在 HTML5 中创建实时通知。我正在使用的代码让用户在一个框中写一些文本,当按钮“开始!”选中,其他活动用户将收到该短信。 Pusher 还有一个调试控制台来检查所有连接和传输的数据。
我遇到的问题是,当我单击发送通知的按钮时,我可以在调试控制台中看到它,但它不包含任何文本,它是一个空 JSON,并且什么也没有显示其他用户。 这是发送的 JSON:
{
"message": null
}
我要主要文件。 index.html 位于:mywebsite.com/notifications/index.html 这里是:
<html>
<head>
<title>Realtime Notifications</title>
<script src="//js.pusher.com/2.2/pusher.min.js" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
</head>
<body>
<div class="notification"></div>
<input class="create-notification" placeholder="Send a notification :)"></input>
<button class="submit-notification">Go!</button>
<script>
var pusher = new Pusher('MY APP KEY');
var notificationsChannel = pusher.subscribe('notifications');
notificationsChannel.bind('new_notification', function(notification){
var message = notification.message;
$('div.notification').text(message);
});
var sendNotification = function(){
// get the contents of the input
var text = $('input.create-notification').val();
// POST to our server
$.post('http://mywebsite.com/notifications/notification', {message: text}).success(function(){
console.log('Notification sent!');
});
};
$('button.submit-notification').on('click', sendNotification);
</script>
</body>
</html>
还有一个 index.php,位于:mywebsite.com/notifications/notification/index.php 这是代码:
<html>
<head>
<title> notification index </title>
</head>
<body>
<?php
require(dirname(__FILE__).'/../vendor/autoload.php');
$pusher = new Pusher('MY APP KEY', 'MY APP SECRET', 'MY APP ID');
// trigger on 'notifications' an event called 'new_notification' with this payload:
$text = $_POST['message'];
$data['message'] = $text;
$pusher->trigger('notifications', 'new_notification', $data);
?>
</body>
</html>
在添加 sendNotification 函数之前,该代码正在运行。因此,这绝对不是与缺少库、依赖项等相关的问题。 这可能是与权限相关的问题吗? 提前感谢您的帮助。
【问题讨论】:
标签: javascript php json push-notification pusher