简体   繁体   English

使用express.js从Twitter流中将数据从我的服务器流式传输到AJAX调用

[英]streaming data from twitter stream to AJAX call from my server using express.js

I am creating a Node web app using Express.js and twitter.js. 我正在使用Express.js和twitter.js创建一个Node Web应用程序。 I am sucsessfully recieving the twitter stream on my server, however, I am unable to stream that data to my client(AJAX call). 我已成功在服务器上接收到Twitter流,但是,我无法将该数据流传输到客户端(AJAX调用)。 When attempting to do so I always recieve this error on the second tweet : 尝试这样做时,我总是在第二条推文上收到此错误:

Error: Can't set headers after they are sent.

I know what causes this error ( res.send calls res.end ), but I am not sure how to get around it using Express. 我知道导致此错误的原因( res.send调用res.end ),但是我不确定如何使用Express解决该错误。

HERE IS MY EXPRESS SERVER 这是我的特快服务器

app.get('/gamepage/twitter', function(req, res, next){
client.stream('statuses/filter.json', params, function(stream){
    stream.on('data', function(event){
        console.log(event);
        //game module calls
        mix.getTweets(event.user.name, event.text); //Store tweet
        mix.getLastPrice(); // set the last price
        let last = mix.lastPrice;
        console.log('last price: '+ last);
        //mix.trackScores(mix.tweets.user); // look for cmd and change score accordingly

        // send object
        res.send({
            allTweets: mix.tweets,
            price: mix.price,
            scores: mix.scores
        });

    });
    stream.on('error', function(error){
        throw error;
    });

HERE IS MY AJAX 这是我的阿贾克斯

function getTweets () {

let xhrTweets = new XMLHttpRequest();
xhrTweets.open('GET', 'http://localhost:3003/gamepage/twitter', true);
xhrTweets.addEventListener('load', function(){
    console.log('loaded');
    //getPrice();
    let data = JSON.parse(xhrTweets.response);
    let mostRecent = 0//data.allTweets.length-1;
    console.log(data);
    console.log(data.allTweets);
    console.log(data.price);
    console.log(data.scores);

    let oldContainer = document.getElementById('gameresults');
    let newContainer = document.createElement('div');
    newContainer.setAttribute('id', 'gameresults');
    document.getElementById('resultcontainer').replaceChild(newContainer, oldContainer);

    let resultDisplay = document.createElement('div');
    resultDisplay.setAttribute('class', 'tweetContainer');
    let newTweet = document.createElement('p');
    let tweetUser = document.createElement('p');
    let newPrice = document.createElement('p');
    let displayText = document.createTextNode(data.allTweets[mostRecent].text);
    let displayUser = document.createTextNode(data.allTweets[mostRecent].user);
    let displayPrice = document.createTextNode(data.price);

    newContainer.appendChild(resultDisplay);
    resultDisplay.appendChild(tweetUser);
    resultDisplay.appendChild(newTweet);
    resultDisplay.appendChild(newPrice);
    newTweet.appendChild(displayText);
    tweetUser.appendChild(displayUser);
    newPrice.appendChild(displayPrice);

});

//xhrTweets.timeout = 15*1000;
//xhrTweets.addEventListener('timeout', function(data){
//  console.log('timeout');
//  console.error(data);

//});

xhrTweets.send();

} }

So how do I stream data from twitter Through express and into my AJAX call while keeping the stream open for later data? 那么,如何在保持流开放以供以后的数据流的同时, 通过 twitter 通过 express流式传输数据到我的AJAX调用中?

The problem is that you try to do res.send multiple times, every time a chunk of data is received from the stream. 问题是,每次从流中接收到大量数据时,您尝试多次进行res.send。 Check out this SO question I think it can help you 看看这个SO问题,我认为它可以为您提供帮助

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

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