当我以隐身模式打开两个浏览器窗口时(不同的 session )。
我得到了一个流,效果很好...新推文按预期从 Twitter 流 API 中传入。问题是,当其他窗口加载页面时,第一个隐身窗口会断开连接。
这是我正在使用的代码。我不确定 Twitter 是否可能不允许同时流,或者是否符合我的逻辑。
var mod = module.exports = {}
, twitter = require('twitter')
, c = console;
var io
, sock
, intv = {};
function onNewsInit(data){
setTimeout(function(){
stream(streamQuery);
}, 1000 * 5);
}
function stream(query){
c.log('stream query', query);
twit.stream('statuses/filter', {track:query}, function(stream) {
stream.on('data', function (data) {
c.log(data);
var item = getItem(query, data);
c.log('stream item', item);
if ( item ) {
sock.emit('twitter:item', { item: item });
}
});
stream.on('end', function (response) {
// Handle a disconnection
c.log('stream end');
});
stream.on('destroy', function (response) {
// Handle a 'silent' disconnection from Twitter, no end/error event fired
c.log('stream destroy');
});
// Disconnect stream after five seconds
sock.on('news:end', stream.destroy);
sock.on('disconnect', stream.destroy);
});
}
mod.register = function register(soc, sio) {
c.log('news register');
io = sio;
sock = soc;
sock.on('news:init', onNewsInit);
sock.on('news:end', onNewsEnd);
};
app.js
io.sockets.on('connection', function(sock){
news.register(sock, io);
});
一个客户端能够断开另一个客户端的连接有什么原因吗?
最佳答案
事实证明 Twitter Stream API 只允许一个同时连接。
如下所述: https://dev.twitter.com/docs/streaming-apis/streams/public
Connections
Each account may create only one standing connection to the public endpoints, and connecting to a public stream more than once with the same account credentials will cause the oldest connection to be disconnected.
Clients which make excessive connection attempts (both successful and unsuccessful) run the risk of having their IP automatically banned.
关于node.js - socket.io 正在断开我的 Node Twitter 应用程序中所有其他已连接用户的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20717718/