zoukankan      html  css  js  c++  java
  • [Node.js]33. Level 7: Persisting Questions

    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.

  • 相关阅读:
    sslforfree的证书合并成类似于certbot的ssl证书文件
    190129 胡思乱想
    Android deprecated apache module (HttpClient, HttpResponse, etc.)
    黑阀 adb 命令
    windows10 vs2019 + opencv 3.4.7环境搭建
    ASP.NET MVC 微信公众号支付,微信公众平台配置
    jQuery 滚动条 滚动到底部(下拉到底部) 加载数据(触发事件、处理逻辑)、分页加载数据
    js显示yyyy年mm日dd天 星期几 的格式日期
    jQuery对 动态添加 的元素 绑定事件(on()的用法)
    Jquery判断页面图片是否加载失败,加载失败则显示默认图片
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3883938.html
Copyright © 2011-2022 走看看