zoukankan      html  css  js  c++  java
  • Node.js使用redis进行订阅发布管理

    redis NPM 官方介绍地址:https://www.npmjs.com/package/redis

    let redis = require('redis');
    
    
    let subscriber;
    let publisher;
    let options = {
        host: "192.168.211.33",
        port: 6379,
        password: "123456"
    }
    subscriber = redis.createClient(options); //创建订阅客户端
    
    subscriber.on("subscribe", (channel, count) => {
        console.log("Subscribe # subscribed to " + channel + ", " + count + " total subscriptions");
    });
    
    subscriber.on('message', (channel, message) => {
        console.log('Message # Channel ' + channel + ': ' + message);
    });
    
    subscriber.on('unsubscribe', (channel, count) => {
        console.log("Unsubscribe # to " + channel + ", " + count + " total subscriptions");
    });
    
    subscriber.on("error", (err) => {
        console.log("redis error: " + JSON.stringify(err));
    });
    
    subscriber.subscribe('main_chat_room'); //订阅信道
    
    
    publisher = redis.createClient(options); //创建发布客户端
    publisher.publish('main_chat_room', "BBQ IS HERE"); //发布消息
    
    
    setTimeout(() => {
        subscriber.unsubscribe('main_chat_room'); //延时取消信道订阅
    }, 15000);
    
    
    setTimeout(() => {
        subscriber.end();//关闭连接
        publisher.end();
    }, 20000);
    

      

  • 相关阅读:
    期待Eclipse3.4
    Winforms中使用系统托盘图标
    Eclipse下的Struts2开发插件
    彩色 夺冠
    网络&系统
    A Famous Music Composer
    Quick Brown Fox
    解密QQ号——队列
    谁先倒
    bootstrap Table从零开始
  • 原文地址:https://www.cnblogs.com/yourstars/p/11550650.html
Copyright © 2011-2022 走看看