node.js - 用于 node.js 中聊天服务器的 Redis pub/sub

标签 node.js redis socket.io

我正在尝试使用 Redis Cookbook 示例:

var http = require('http'),
io = require('socket.io')
fs = require('fs'),
redis = require('redis'),
rc = redis.createClient(9189, "pike.redistogo.com");
rc.auth("passwd", function() {
    console.log("Connected! to redistogo!");});

rc.on("connect", function() {
    rc.subscribe("chat");
    console.log("rc connect event");
});

我在这里成功了,但从未收到“消息”。

rc.on("message", function (channel, message) {
 console.log("Sending: " + message);
 socketio.sockets.emit('message', message);
});

webpage = http.createServer(function(req, res){
console.log('webpage request starting...');

fs.readFile('./index.htm', function(error, content) {
    if (error) {
        res.writeHead(500);
        res.end();
    }
    else {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(content, 'utf-8');
     }
 });
 });

 webpage.listen(7777);

我的客户端 index.htm 是这个

<!docttype html>
<html lang="en">
<head>
    <script src ="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">     </script>
       <script src="http://www.heaphash.com:7777/socket.io/socket.io.js"></script>
       <script>
        var socket = io.connect('www.heaphash.com', { port: 7777});
            socket.on('message', function(data){
               var li = new Element('li').insert(data);
               $('messages').insert({top: li});
           }
        </script>
        <meta charset="utf-8">
        <title>Chat with Redis</title>
 </head>
 <body>
        <ul id="messages">
            <!-- chat messages go here -->
        </ul>
        <form id="chatform" action="">
         <input id="chattext" type="text" value="" />
         <input type="submit" value="Send" />
        </form>
        <script>
                $('#chatform').submit(function(){
                        socket.emit('message', $('chattext').val());
                        $('chattext').val(""); //cleanup the field
                        return false;
                });
        </script>
</body>
</html>

客户端如何发布到特定的 Redis“聊天” channel 。

最佳答案

如果你在你的 node.js 程序中使用 redis pub/sub 功能,你应该使用一个 redis 客户端连接来监听某个 channel ,第二个 redis 客户端连接用于发送普通命令和/或发布消息到你的 channel .来自 node_redis文档:

When a client issues a SUBSCRIBE or PSUBSCRIBE, that connection is put into "pub/sub" mode. At that point, only commands that modify the subscription set are valid. When the subscription set is empty, the connection is put back into regular mode.

If you need to send regular commands to Redis while in pub/sub mode, just open another connection.

您的问题也与这些问题有关:

关于node.js - 用于 node.js 中聊天服务器的 Redis pub/sub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7463714/

相关文章:

node.js - package-lock.json 中的 "requires: true"有什么作用

node.js - 为什么我收到 : 502 Bad Gateway?

java - 在java中创建自己的后端有意义吗?

javascript - node.js 和 socket.io。 websocket 的传输类型配置?

node.js - 我怎样才能在 express 上建立一个永远不会响应的端点

mysql - 将json插入到nodejs中的mysql行

caching - 如何使用 UUID 配置 Redis 缓存以进行 session 管理

java - 绝地武士-Redis : Does maximum number of clients increases chances of response time from Redis?

javascript - 页面重新加载后连接多个对等连接

node.js - nodejs- socket.io 服务器为所有用户推送相同的数据