Let's go back to our live-moderation app and add some persistence, first to the questions people ask.

Use the lpush command to add new questions to the list named questions. Do this inside thequestion listener.

var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app); var redis = require('redis');
var redisClient = redis.createClient(); io.sockets.on('connection', function(client) {
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
}); client.on('question', function(question) {
client.get('question_asked', function(asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question); // add the question to the list here
redisClient.lpush('questions', question);
}
});
});
});

Now that we have questions stored in redis, let's emit them whenever a new client connects to the server through socket.io.

Use the lrange command to retrieve an array of questions that represent the questions list in redis. Inside of the lrange callback, use forEach to loop through each question and emit it on the client. Remember, don't use broadcast.emit because we only want to send the questions to the client that is connecting to the server.

var express = require('express');
var app = express.createServer();
var socket = require('socket.io');
var io = socket.listen(app); var redis = require('redis');
var redisClient = redis.createClient(); io.sockets.on('connection', function(client) {
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
}); redisClient.lrange('questions', 0, -1, function(err, messages){
messages.forEach(function(message){
client.emit('question', message);
});
}); client.on('question', function(question) {
client.get('question_asked', function(asked) {
if(!asked) {
client.set('question_asked', true);
client.broadcast.emit('question', question); redisClient.lpush("questions", question);
}
});
});
});

Great work! One last thing though, since every time a new question comes in we store it in thequestions list, we might run into a problem where there are just too many questions stored in that list.

Add a callback to the lpush command, and inside that callback use the ltrim command to make sure the questions list always has at most 20 items.

最新文章

  1. 先说IEnumerable,我们每天用的foreach你真的懂它吗?
  2. Linux网络管理1---(Linux配置IP地址,ifconfig、配置文件)
  3. notepad++快捷键大全
  4. 五步搞定Android开发环境部署
  5. 下列哪项不属于jdk1.6垃圾收集器?
  6. Android系统自带样式(android:theme)详解-(转)
  7. 15个关于Chrome的开发必备小技巧
  8. Hololens开发笔记之Gesture手势识别(单击,双击)
  9. db2日期和时间常用汇总
  10. PHP四种传参方式
  11. cocos2d-x3.0 SpriteFrameCache
  12. 浅析指针(pointer)与引用(reference)
  13. nyoj_323:Drainage Ditches(网络流入门)
  14. UITableView 的使用小点
  15. Java 面向对象的基本特征
  16. git代理配置
  17. Pyjwt ,python jwt ,jwt
  18. ②泡茶看<数据结构>,喜欢看源码-栈ADT
  19. HDU3306 Another kind of Fibonacci 矩阵
  20. LEO原创-FMX之你不知道的ARC

热门文章

  1. [BZOJ5305][HAOI2018]苹果树(DP)
  2. Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�
  3. 在Hexo中渲染MathJax数学公式
  4. 【对比分析五】CSS阻塞和JS阻塞
  5. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分
  6. Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题
  7. Python学习笔记(一):Python基础学习
  8. 华为S5300系列交换机V100R006SPH019升级补丁
  9. Iterative (non-recursive) Quick Sort
  10. hybrid App h5二级页面返回的时候保持与一级页面浏览的位置一致