基于nodeJS+redis 实现聊天室应用

宗政松
2023-12-01

var request=require('request');
var url=request('url');
app.get('/tweets/:username',function(req,response){
    var username=req.params.username;
    options={
        protocol:"http",
        host:'api.twitter.com',
        pathname:'/1/statuses/user_timeline.json',
        query:{screen_name:username,count:10}
    }
    var twitterUrl=url.format(options);
    request(twitterUrl).pipe(response);
})


app.get('/tweets/:username',function(req,response){
    request(url,function(err,res,body){
        var tweets=JSON.parse(body);
        response.render('tweets.ejs',{tweets:tweets,name:username});
    })
})


npm install socket.io

var socket=require('socket.io');
var app=express.createServer();
var io=socket.listen(app);

io.sockets.on('connection',function(client){
    console.log('Client connected');
    client.emit('message',{hello:'world'});
});

<script src="socket.io/socket.io.js"></script>
<script>
var server=io.connect('http://localhost:8080');
server.on('message',function(data){
    alert(data.hello);
})
</script>


聊天室:

io.sockets.on('connection',function(client){
    client.on('message',function(data){
        console.log(data);
    })
})


var server=io.connect('http://localhost:8080');
$('#chat_form').submit(function(e){
    var message=$('#chat_input').val();
    socket.emit('message',message);
})

io.sockets.on('connection',function(client){
    client.on('join',function(name){
        client.set('nickname',name);
    })
    client.on('messages',function(data){
        client.get('nickname',function(err,name){
            client.broadcast.emit("chat",name+": "+message);
        })
    })
})


var server=io.connect('http://localhost:8080');
server.on('connect',function(data){
    $('#status').html('Connected to chattr');
    nickname=prompt("what your nickname");
    
    server.emit('join',nickname);
})

redis 实现消息服务器
npm install redis

var redis=requrie('redis');
var client=redis.createClient();

client.set("message1","Hello,yes this is dog");
client.set("message2","Hello no this is spider");


client.get("message1",function(err,replay){
    console.log(replay);
})


var message="Hello this is dog";
client.lpush("message",message,function(err,replay){
    console.log(reply);
})

var message="Hello,no this is spider";
client.lpush("messages",message,function(err,reply){
    console.log(reply);
})


var message="Oh sorry,wrong number";
client.lpush("messages",message,function(err,reply){
    client.ltrim("message",0,1);
})

client.lrange("message",0,-1,function(err,messages){
    console.log(messages);
})

var redisClient=redis.createClient();

var storeMessage=function(name,data){
    var message=JSON.stringify({name:name,data:data});

    redisClient.lpush("messages",message,function(err,response){
        redisClient.ltrim("message",0,10);
    });


    message.push({name:name.data:data});
    
    if(messages.length>10){
        messages.shift();
    }
}


client.on('join',function(name){
    redisClient.lrange("message",0,-1,function(err,messages){
        messages=messages.reverse();

        messages.forEach(function(message){
            message=JSON.parse(message);
            client.emit("messages",message.name+": "+messages.data);
        })
    })
})


通讯录实现:

client.sadd("names","Dog");
client.sadd("names","Spider");
client.sadd("names","Gregg");

client.srem("names","Spider");
client.smembers("names",function(err,names){
    console.log(names);
})

client.on("join",function(name){
    client.broadcast.emit("add chatter",name);
    redisClient.sadd("chatters",name);
});

server.on('add chatter',insertChatter);

var insertChatter=function(name){
    var chatter=$('<li>'+name+'</li>').data('name',name);
    $('#chatters').append(chatter);
}

client.on("join",function(name){
    client.broadcast.emit("add chatter",name);
    redisClient.smembers("names",function(err,names){
        names.forEcah(function(name){
            client.emit('add chatter',name);
        });
    })
    redisClient.sadd("chatters",name);
});


client.on("disconnect",function(name){
    client.get('nickname',function(err,name){
        client.broadcast.emit("remove chatter",name);
        redisClient.srem("chatters",name);
    })
})

server.on("remove chatter",removeChatter);

var removeChatter=function(name){
    $('#chatters li[data-name='+name+']').remove();
}

 类似资料: