I'm trying to build a simple Node app using Express, Socket.io, and the ntwitter module so you can just search for a term (that's req.params.searchTerm below) and ntwitter will search for it and output the stream via socket.io.
我正在尝试使用Express Socket构建一个简单的节点应用。io和ntwitter模块,这样你就可以搜索一个词(那是req.params)。)和ntwitter将搜索它并通过socket.io输出流。
I'm running into errors, though, with multiple windows open, or closing a window and then opening a window immediately afterwards. Sometimes the search term is not updated, sometimes some kind of socket.io error is actually thrown. Could someone clarify the right way to do this or shed some light to my error? Thanks in advance. Code below.
但是,我遇到了错误,打开多个窗口,或者关闭一个窗口,然后立即打开一个窗口。有时搜索词没有更新,有时是某种套接字。io错误实际上被抛出。有人能澄清一下正确的方法吗?或者能解释一下我的错误吗?提前谢谢。下面的代码。
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
app.get('/:stream', function(req, res) {
res.render('stream', { title: 'Search Twitter | ' + req.params.searchTerm });
io.sockets.on('connection', function(socket) {
var publishFlag = true;
setInterval(function() {
publishFlag = true;
}, 1000);
twit.stream('statuses/filter', { track: [req.params.searchTerm] },
function(stream) {
stream.on('data', function(tweet) {
if(publishFlag) {
socket.emit('tweet', tweet);
publishFlag = false;
}
});
}
);
});
});
1 个解决方案
#1
4
It think that is somewhat what you are trying to do :
它认为这就是你想做的:
Server side :
服务器端:
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
app.get('/:stream', function(req, res) {
res.render('stream', { title: 'Search Twitter | ' + req.params.searchTerm });
});
io.sockets.on('connection', function(socket) {
socket.on('stream', function(searchTerm){
twit.stream('statuses/filter', { track: [searchTerm] },
function(stream) {
stream.on('data', function(tweet) {
socket.emit('tweet', tweet);
});
}
);
};
});
client side (/:stream)
客户端(/:流)
var socket = io.connect('http://localhost');
socket.on('tweet', addTweetOnPage);
socket.emit('stream', searchTerm);
#1
4
It think that is somewhat what you are trying to do :
它认为这就是你想做的:
Server side :
服务器端:
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
app.get('/:stream', function(req, res) {
res.render('stream', { title: 'Search Twitter | ' + req.params.searchTerm });
});
io.sockets.on('connection', function(socket) {
socket.on('stream', function(searchTerm){
twit.stream('statuses/filter', { track: [searchTerm] },
function(stream) {
stream.on('data', function(tweet) {
socket.emit('tweet', tweet);
});
}
);
};
});
client side (/:stream)
客户端(/:流)
var socket = io.connect('http://localhost');
socket.on('tweet', addTweetOnPage);
socket.emit('stream', searchTerm);