读取推送器数据

Reading pusher data

谁能帮我读取示例回调中返回的推送消息。我直接从他们的网站上拿了这个例子,但是警告框只显示 'undefined',但是 console.log 输出一切正常。我正在通过帐户控制面板中的 API 控制台发送测试消息。我正在相应地更改密钥和频道,下面的代码是为了 Whosebug 的好处。

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="//js.pusher.com/2.2/pusher.min.js"></script>
  <script>
    // Enable pusher logging - don't include this in production
    Pusher.log = function(message) {
      if (window.console && window.console.log) {
        window.console.log(message);
      }
    };

    var pusher = new Pusher('MYAPIKEY');
    var channel = pusher.subscribe('test_channel');
    channel.bind('my_event', function(data) {
      alert(data.message);
    });
  </script>
</head>

我试过 data[0].messagedata.data,但总是 returns 未定义。如果我警告 data,那么它会按预期 returns [object] [object],所以有些东西我无法显示。

data 变量的结构取决于您通过 Pusher 发送的事件数据。您可以使用 Pusher 调试控制台对此进行测试。下图中的示例将起作用,并在您问题中的代码的警报中显示 hello