Pusher无法发送或接收消息

时间:2017-04-25 03:42:41

标签: javascript php pusher

我在PHP中使用这个推送实时聊天库:

https://github.com/pusher-community/pusher-realtime-chat-widget,我已完成安装此操作的所有步骤,但当我发送消息时,响应返回:

Chat message sent. Result: success : 

{
 "activity": {
    "id": "58fec2f198619",
    "body": "hello world",
    "published": "Tue, 25 Apr 2017 03:30:57 +0000",
    "type": "chat-message",
    "actor": {
      "displayName": "test",
      "objectType": "person",
      "image": {
        "url": "http:\/\/www.gravatar.com\/avatar\/61804bdff74c1969d90a1cac142a3b20?s=80&d=mm&r=g",
        "width": 48,
        "height": 48
      }
    }
  },
  "pusherResponse": {
    "body": "Unknown auth_key\n",
    "status": 400
  }
}

JS:

<script>
    $(function () {
        var pusher = new Pusher("API_KEY", {
            cluster: 'ap2',
            encrypted: true
        });
        var chatWidget = new PusherChatWidget(pusher, {
            appendTo: "#pusher_chat_widget",
            channelName: 'site-wide-chat-channel'
        });
    });
</script>

PHP:

<?php
require_once('./vendor/autoload.php');
require_once('Activity.php');
require_once('config.php');

date_default_timezone_set('UTC');

$chat_info = $_POST['chat_info'];

$channel_name = null;

if (!isset($_POST['chat_info'])) {
    header("HTTP/1.0 400 Bad Request");
    echo('chat_info must be provided');
}

if (!isset($_SERVER['HTTP_REFERER'])) {
    header("HTTP/1.0 400 Bad Request");
    echo('channel name could not be determined from HTTP_REFERER');
}

$channel_name = get_channel_name($_SERVER['HTTP_REFERER']);
$options = sanitise_input($chat_info);

$activity = new Activity('chat-message', $options['text'], $options);

$optionsIs = array(
    'cluster' => 'ap2',
    'encrypted' => true
);
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID, $optionsIs);
$data = $activity->getMessage();
$response = $pusher->trigger('site-wide-chat-channel', 'chat_message', $data, null, true);

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');

$result = array('activity' => $data, 'pusherResponse' => $response);
echo(json_encode($result));

function get_channel_name($http_referer)
{
    // not allowed :, / % #
    $pattern = "/(\W)+/";
    $channel_name = preg_replace($pattern, '-', $http_referer);
    return $channel_name;
}

function sanitise_input($chat_info)
{
    $email = isset($chat_info['email']) ? $chat_info['email'] : '';

    $options = array();
    $options['displayName'] = substr(htmlspecialchars($chat_info['nickname']), 0, 30);
    $options['text'] = substr(htmlspecialchars($chat_info['text']), 0, 300);
    $options['email'] = substr(htmlspecialchars($email), 0, 100);
    $options['get_gravatar'] = true;
    return $options;
}

?>

我已经验证了我的API KEY和秘密,我不知道我错过了什么?

0 个答案:

没有答案