【问题标题】:PubNub Twitter Data Stream setup doesn't return dataPubNub Twitter 数据流设置不返回数据
【发布时间】:2017-02-08 01:17:38
【问题描述】:

尝试使用 D3 在世界地图上映射推文,并首次使用 PubNub 获取实时数据流。设置时遇到问题并得到以下代码:

var margin = {top: 20, right: 20, bottom: 20, left: 20};

var w = 1100 - margin.left - margin.right,
    h = 900 - margin.top - margin.bottom;

var svg = d3.select("#chart")
            .append("svg")
              .attr("width", w + margin.left + margin.right)
              .attr("height", h + margin.top + margin.bottom)
            .append("g")
              .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var geoData = "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json";

d3.json(geoData, function(data){

    var geo = data.features;

    var projection = d3.geo.mercator()
                       .scale(150)
                       .translate([w/2,h/2]);

    var path = d3.geo.path()
                     .projection(projection);

    svg.selectAll("path")
       .data(geo)
       .enter()
       .append("path")
       .attr("fill", "#95E1D3")
       .attr("stroke", "#34495e")
       .attr("stroke-width", 0.5)
       .attr("class", function(d){ return d.properties.name})
       .attr("d", path);

var pubnub = new PubNub({

     subscribeKey : "my key"

   });

    pubnub.subscribe({

    channels: ['pubnub-twitter'],

    withPresence: true

     });

   pubnub.addListener({

    message: function(m) {

        console.log(m);    

    },

   presence: function(p){

     console.log(p);

   },

   status: function(s){

     console.log(s);

   }

  })


})

我希望它将消息打印到控制台,但没有任何反应。使用 PubNub v4。谁能帮帮我?

这是一个工作的 Codepen:

http://codepen.io/chemok78/full/mRjXzb/

【问题讨论】:

标签: javascript api d3.js twitter pubnub


【解决方案1】:

Twitter 示例 Feed 流式推文

以下示例将打印console.log() 中的所有推文。您可以通过单击“运行代码 sn-p”按钮自行测试。您将在控制台中看到一连串推文。

// Create PubNub Socket Handler
const pubnub = new PubNub({
    publishKey   : 'empty'
,   ssl          : true
,   subscribeKey : 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe'
});

// Subscribe to Twitter feed
console.log("Subscribing to Live Twitter Stream.");
pubnub.subscribe({ channels: ['pubnub-twitter'] });

// Add Socket Event Function Handlers
pubnub.addListener({
    status  : statusEvent => console.log(statusEvent)
,   message : message     => console.log(message)
});
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.4.2.min.js"></script>

PubNub 在 2014 年发布了一篇博客,展示了 how to analyze tweets from Twitter's Firehose

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多