我有一个附加到twitter api的套接字流,我需要找到一种方法来打开和关闭它。我试图使用一个数组作为一种计数器和一个if语句来打破流填充到10位的位置数据。
var startArray = [];
io.on('connection', function(socket) {
if(startArray.length < 10){
console.log('a user connected');
// socket.on('twitter-stream', function(input) {
client.stream('statuses/filter', {locations:'-180,-90,180,90'}, function(s) {
stream = s;
stream.on('data', function(data) {
// console.log(data);
// Does the JSON result have coordinates
// console.log(data.text)
if (data.coordinates){
if (data.coordinates !== null ){
//If so then build up some nice json and send out to web sockets
var outputPoint = {"lat": data.coordinates.coordinates[0],"lng": data.coordinates.coordinates[1]};
startArray.push(outputPoint);
io.emit("twitter-stream", outputPoint);
console.log(outputPoint);
console.log(startArray.length);
// console.log(data.text);
// console.log(data.text);
//Send out to web sockets channel.
io.emit('twitter-stream', outputPoint);
}
}
});
// }); // end client stream
}) // end socket 'activate'
};//end of if statement
});//end of sockets`
^^ 这部分在我的server.js中 这部分是我的sockets.js它应该包含一个能够切换我的流状态的按钮,但这似乎不起作用,无论是打开还是关闭流。 VV
var socket = io();
var locationsArray = [];
$(document).on('click', '#start-stream', function(){
console.log('start-stream');
socket.on('twitter-stream', function(tweet) {
//only selects tweets with coordinates
console.log(tweet);
var coolShit = tweet;
locationsArray.push(coolShit);
});
})