2015-10-24 47 views
1

我使用Pusher在HTML5中創建實時通知。我使用的代碼讓用戶在一個框中寫入一些文本,當按鈕「Go!」時被選中時,其他活動用戶將收到該短信。 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函數之前工作。所以這絕對不是缺少庫,依賴等問題。 它可能是一個權限相關的問題? 在此先感謝您的幫助。

回答

0

發現問題。這是在index.html的,當我嘗試發佈到服務器:

// POST to our server 
    $.post('http://mywebsite.com/notifications/notification' 

我需要指定的index.php:

// POST to our server 
    $.post('http://mywebsite.com/notifications/notification/index.php' 

以爲是要自動找到它:)