简体   繁体   English

将Pusher集成到简单的php Web应用程序

[英]Integrating Pusher to a simple php web app

Hello stackoverflow community, I am learning how to implement Pusher API http://pusher.com into this simple web chat app. 您好stackoverflow社区,我正在学习如何在此简单的Web聊天应用程序中实现Pusher API http://pusher.com I was following a video tutorial and did every step correctly, but when I try to send a msg, it will be displayed correctly on my web browser, but will not be displayed or refreshed on another web browser. 我正在观看视频教程,并且正确地完成了每个步骤,但是当我尝试发送消息时,它将在我的Web浏览器上正确显示,但是在另一个Web浏览器上不会显示或刷新。 I will add my 2 php files, they are short. 我将添加2个php文件,它们很短。

<!doctype html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>Pusher Messenger</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
        <script src="https://js.pusher.com/3.1/pusher.min.js"></script>
        <script>

        // Enable pusher logging - don't include this in production
        //Pusher.logToConsole = true;

        var pusher = new Pusher('your pusher key here', {
          encrypted: true
        });

        var channel = pusher.subscribe('channel_pusher');
        channel.bind('new_message', function(response){
          $('#sent_messages').append('<li>' + response.message + '</li>');
        });

        $(function(){
            $('form').submit(function(){
                $.post('ajax.php', { msj : $('#input_mensaje').val() }, function(response){
                    //funcion de callback
                    $('#sent_messages').append('<li>' + response.message + '</li>');
                }, 'json');

                return false;
            });
        });
        </script>
    </head>
    <body>
        <form action="" methor="post">
            <input type="text" id="input_mensaje" />
            <input type="submit" class="submit" value="Send" />
        </form>

        <ul id="sent_messages">
            <!-- Sent messages will be shown here -->
        </ul>
    </body>
    </html>

And this is my ajax.php file: 这是我的ajax.php文件:

<?php
    require('lib/Pusher.php');

    $options = array(
        'encrypted' => true
    );

    $message = $_POST['msj'];

    $pusher = new Pusher(
        'code provided by pusher',
        'code provided by pusher',
        'code provided by pusher',
        $options
      );

    $pusher->trigger(
        'channel_pusher',
        'new_message',
        array('message' => $message)
    );

    echo json_encode(array('message' => $message));
?>

I have just tested your code with my own app key and it seems to work fine. 我刚刚用我自己的应用程序密钥测试了您的代码,它似乎工作正常。 I did notice, however, that while you included your app key and secret in the ajax.php you quoted (which should normally be avoided), your HTML only contains 但是我确实注意到,虽然您在引号的ajax.php中包含了应用程序密钥和机密(通常应避免使用),但HTML仅包含

var pusher = new Pusher('your pusher key here',

Please make sure to provide the app key in both files so that both can actually communicate with Pusher. 请确保在两个文件中都提供应用程序密钥,以便两者都可以与Pusher实际通信。

Another thing to note is that your ajax.php currently passes a message submitted from the page both to Pusher and also back to the page itself. 还要注意的另一件事是,您的ajax.php当前将从页面提交的消息传递给Pusher,也返回给页面本身。 The upshot of this is that the page that submitted the message will in fact append it twice, once from the response returned by ajax.php and once upon the new_message event being received from Pusher. 这样做的结果是,提交该消息的页面实际上将附加两次,一次是从ajax.php返回的响应中,一次是在从Pusher接收到new_message事件时。 This may or may not be what you had in mind. 这可能是您想的,也可能不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM