2011-09-18 19 views
9

मैं Redis कुकबुक उदाहरण काम करने के लिए कोशिश कर रहा हूँ: "। संदेश"Redis पब/उप

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 कैसे एक ग्राहक एक विशिष्ट Redis "चैट" चैनल पर प्रकाशित करता है इस

<!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> 

है।

उत्तर

7

यदि आप अपने node.js प्रोग्राम में रेडिस पब/उप कार्यक्षमता का उपयोग कर रहे हैं तो आपको कुछ चैनलों और दूसरे रेडिस क्लाइंट कनेक्शन को सामान्य आदेश भेजने और/या अपने चैनल में संदेश प्रकाशित करने के लिए एक रेडिस क्लाइंट कनेक्शन समर्पित करना चाहिए)। node_redis डॉक्स से:

एक ग्राहक जारी करता है एक सदस्यता या PSUBSCRIBE हैं, तो उस कनेक्शन "पब/उप" मोड में डाल दिया है। उस बिंदु पर, केवल सदस्यता सेट को संशोधित करने वाले आदेश वैध हैं। जब सदस्यता सेट खाली होता है, तो कनेक्शन नियमित मोड में वापस रखा जाता है।

यदि आपको पब/उप मोड में में नियमित आदेश भेजना है, तो बस एक और कनेक्शन खोलें।

आपकी समस्या भी इन सवालों से संबंधित है:

1

मुझे विश्वास है कि उस किताब से उदाहरण कुछ याद आ रही है, मैं भी उस किताब पढ़ सकते हैं और सोचा। आपने रेडिस चैनल की सदस्यता ली है और सर्वर की ओर से संदेशों की प्रतीक्षा कर रहे हैं, लेकिन आप उस चैनल को कभी प्रकाशित नहीं करते हैं। गायब क्या है एक ईवेंट श्रोता है, जब कोई वेबसाईट संदेश होता है, तो आप उस संदेश को रेडिस चैनल पर प्रकाशित करते हैं।

संबंधित मुद्दे