【发布时间】:2013-05-30 17:17:59
【问题描述】:
我需要为我的应用程序(Ruby On Rails)添加实时,所以,我认为更好的方法是使用 node.js + socket.io + redis。
我在后端 (node.js) 中有这个 application.js 文件
var app = require('http').createServer();
var io = require('socket.io');
var redis = require('redis').createClient();
var _ = require('underscore')._;
io = io.listen(app);
io.configure(function() {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
io.set("close timeout", 10);
io.set("log level", 1);
})
redis.subscribe('rt-change');
io.on('connection', function(socket) {
redis.on('message', function(channel, message) {
socket.emit('rt-change', message)
});
});
var port = process.env.PORT || 5001;
app.listen(port);
以及前端的messages.js
var socket = io.connect('http://localhost:5001/socket.io');
socket.on('rt-change', function (data) {
console.log(data);
});
我正在使用 node application.js 命令启动 application.js,它可以正常工作!
MacBook-Pro-Zhirayr:rt zhirayr$ node application.js info - socket.io 启动
但是当我尝试从 Rails 应用程序使用 redis ($redis.publish 'rt-change', {hello:'world'}) 发送消息时,我的浏览器不会在控制台中记录任何内容。 我敢肯定,建立了来自浏览器的连接,因为当我停止节点时,它会引发连接被拒绝错误。而且我确定 redis 和节点之间的连接已建立,导致 application.js 中的 console.log(message) 记录它。 但是浏览器中的 console.log 不会记录任何内容。
有什么想法吗?
谢谢。
#Antoine 的 UPD
在 application.js 中添加了 console.log io.on('连接', function(socket) { redis.on('message', function(channel, message) { console.log('来自 redis 的新消息'); socket.emit('rt-change', 消息); }); });
当 r.publish 'rt-change', {:hello=>'world'} 被执行时,节点记录如下:
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
new message from redis
很奇怪,1条消息节点记录了11次。
【问题讨论】:
-
你从哪里发送这个消息
$redis.publish 'rt-change', {hello:'world'}? -
能否在redis订阅回调中加入日志以确保触发?
-
浏览器上的 javascript 控制台是否有任何错误?
-
您是否尝试过从 node application.js 发送消息?在 onConnection 回调中放入一些
socket.emit('rt-change', message)。您还 100% 确定浏览器和节点应用程序之间存在连接吗?您将传输设置为 xhr-polling,因此在控制台中您应该会看到“心跳”。
标签: javascript node.js socket.io