我想在我的emit函数中创建一个if语句:如果hashtag是x,则通过此ID发送,如果hashtag为y,则通过该ID发送。我猜这是最好的服务器端。我该怎么做? (我想我需要过滤JSON结果)
这是我的服务器端代码:
var express = require("express");
var app = express();
var Twit = require('twit');
var T = new Twit({
consumer_key: 'JBJUBMi6uBz1j4587RHoWVWX9' // Your Consumer Key
, consumer_secret: 'ChwkS3eXX0obPAGPaw5thpRt8bMr4nbNa7fK3Vc4M8Bieeegr0' // Your Consumer Secret
, access_token: '150020683-nztFnojPcUyxUVXllWXFJ9p99JAaTA6MFE6tr5Wx' // Your Access Token
, access_token_secret: 'jzQMrFnAFjyqMEC7aIDI5hymElWf329Y1dysj0vOAiEGu' // Your Access Token Secret
});
app.use(express.static(__dirname +'/public'));
//routes
app.route('/cool')
.get(function(req,res){
res.sendFile(__dirname+'/public/html/404.html');
});
var server = app.listen(3000, function(){
console.log(" the server is running on port 3000");
})
var socketio = require("socket.io");
var io = socketio.listen(server);
var nbOpenSockets = 0;
var stream = T.stream('statuses/filter', { track: 'liberal, conservative'})
io.on('connection',function(socket){
console.log('Client connected !');
if (nbOpenSockets <= 0) {
nbOpenSockets = 0;
console.log('First active client. Start streaming from Twitter');
stream.start();
}
nbOpenSockets++;
socket.on('disconnect', function() {
console.log('Client disconnected !');
nbOpenSockets--;
if (nbOpenSockets <= 0) {
nbOpenSockets = 0;
console.log("No active client. Stop streaming from Twitter");
stream.stop();
}
});
socket.on('changeBackground', function(newColour) {
console.log(newColour);
socket.emit('setBackground', newColour);
socket.broadcast.emit('setBackground', newColour);
});
stream.on('tweet', function(tweet) {
socket.emit('info', { tweet: tweet });
});
socket.emit('initialize', "initialized");
});
答案 0 :(得分:0)
通过使用IndexOf并为该主题标签分配ID,我能够将id传递给客户端:
stream.on('tweet', function(tweet) {
// console.log(tweet);
if(tweet.text.indexOf('#war') > -1) {
console.log("war")
socket.emit('info', {tweet:"war"});
}