使用PHP从推送器通道获取信息

使用PHP从推送器通道获取信息,php,pusher,Php,Pusher,我对“Pusher”(websocket api)是个新手,我很难理解在将信息发送到频道后如何从服务器获取信息。 例如,这是我的代码: <?php include "pusher/Pusher.php"; ?> <script src="http://js.pusher.com/2.1/pusher.min.js"></script> <script type="text/javascript"> var pusher = new

我对“Pusher”(websocket api)是个新手,我很难理解在将信息发送到频道后如何从服务器获取信息。 例如,这是我的代码:

<?php
    include "pusher/Pusher.php";
?>
<script src="http://js.pusher.com/2.1/pusher.min.js"></script>
<script type="text/javascript">
    var pusher = new Pusher('c77c12b92e38f4156e9c');
    var channel = pusher.subscribe('test-channel');
    channel.bind('my-event', function(data) {
      alert('An event was triggered with message: ' + data.message);
    });
</script>
<?php
    $pusher = new Pusher($config["pusher_key"], $config["pusher_secret"], $config["pusher_id"]);
    $pusher->trigger(channel, 'my-event', array('message' => 'Test Message') );

var推进器=新推进器(“c77c12b92e38f4156e9c”);
var通道=pusher.subscribe('test-channel');
channel.bind('my-event',函数(数据){
警报('使用消息触发事件:'+data.message);
});

您可以在此处找到一个非常简单的示例的来源:

这里的例子是:


您看到的问题是,在页面在浏览器中呈现之前,您正在触发服务器上的事件。因此,浏览器尚未与Pusher建立连接,也未进行订阅。

您可以在此处找到一个非常简单的示例的来源:

这里的例子是:

您看到的问题是,在页面在浏览器中呈现之前,您正在触发服务器上的事件。因此,浏览器尚未与Pusher建立连接,也未进行订阅。

您可以尝试类似的方法,对于我使用composer的php Pusher库

了解更多后续信息

推荐的文件夹结构-

您可以添加以下内容以获得更多UX类外观-


对于我使用composer编写的php推送程序库,您可以尝试类似的方法

了解更多后续信息

推荐的文件夹结构-

您可以添加以下内容以获得更多UX类外观-


<div class="notification">
</div>

<script>
var pusher = new Pusher('APP_KEY');
var notificationsChannel = pusher.subscribe('notification');
notificationsChannel.bind('new_notification', function(notification){
    var message = notification.message;
    toastr.success(message);
});
var sendNotification = function(){
    var text = $('input.create-notification').val();
    $.post('./notification/index.php', {message: text}).success(function(){
        console.log('Notification sent!');
    });
};

$('button.submit-notification').on('click', sendNotification);

</script>
<input class="create-notification" placeholder="Send a notification :)"/>
<button class="submit-notification">Go!</button>
require(dirname(__FILE__).'/../vendor/autoload.php');
$app_id = 'APP_ID';
$app_key = 'APP_KEY';
$app_secret = 'APP_SECRET';
$pusher = new Pusher($app_key, $app_secret, $app_id,array( 'encrypted' => true ));
$data['message'] = $_POST['message'];
$pusher->trigger('notification', 'new_notification', $data);
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css">
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="http://js.pusher.com/2.2/pusher.min.js" type="text/javascript"></script>