我有一个在Rails和Backbone.js上运行的单页应用程序。我正在通过Redis使用Node.js将数据推送并同步到客户端。我正在尝试了解,保护和优化套接字通信。
使用console.log编写时,我看到一些日志行(console.log('Redis connection on ..'))正在重复(请参见下面的内容)。
console.log('Redis connection on ..')
有人能解释我为什么吗?通过我的代码实现,我是否不了解某些特定的Node.js行为?
这是我的Node.js代码:
var io = require('socket.io').listen(3003); var redis = require('redis'); var cookie = require("cookie"); var redis_subs = redis.createClient(6379, "localhost"); var redis_auth = redis.createClient(6379, "localhost"); var authenticated = false; var authentication_status = function (status) { if (status === true) { console.log('Authentication status is true.'); } else { console.log('Authentication status is false.'); } authenticated = status; }; redis_subs.subscribe('application_channel'); io.configure(function () { io.set('authorization', function (data, callback) { console.log('Cross authentication...'); if (data.headers.cookie) { data.cookie = cookie.parse(data.headers.cookie); data.sessionID = data.cookie['_validation_token_key']; redis_auth.hget(["sessionStore", data.sessionID], function (err, session) { if (err || !session) { console.log('Socket.io authorization say false'); return callback(authentication_status(false), false); } else { data.session = JSON.parse(session); console.log('Socket.io authorization say true'); return callback(authentication_status(true), true); } }); } else { console.log('Socket.io authorization say false'); return callback(authentication_status(false), false); } }); }); io.on('connection', function(socket){ if (socket.handshake.session) { var user_socket_channel = socket.handshake.session['user_socket_channel'] redis_subs.on('message', function(redis_channel, rawdata) { console.log('Redis connection on '+redis_channel); console.log('Handshaked Session, authenticated user. All channels allowed.'); var data = JSON.parse(rawdata); if (data.channel) { var socket_channel = data.channel; } else { var socket_channel = user_socket_channel; } var rails_data = data.data; console.log('Socket.io emiting on ['+socket_channel+']') socket.emit(socket_channel, rails_data); }); } else { redis_subs.on('message', function(redis_channel, rawdata) { console.log('Redis connection on '+redis_channel); console.log('No handshaked Session, unauthenticated user. Public channels allowed.'); var data = JSON.parse(rawdata); var rails_data = data.data; console.log('Socket.io emiting on [public]') socket.emit('public', rails_data); }); } });
这是我测试的方式,以及日志变得重复的特定用例:
使用Web浏览器1(例如Firefox)在登录时加载页面;输出:
info - socket.io started Cross authentication... Socket.io authorization say true Authentication status is true. debug - authorized info - handshake authorized bJxm_IcWl2mKZT4Ed-kV debug - setting request GET /socket.io/1/websocket/bJxm_IcWl2mKZT4Ed-kV debug - set heartbeat interval for client bJxm_IcWl2mKZT4Ed-kV debug - client authorized for debug - websocket writing 1::
加载正在用浏览器2注销的同一页面(例如:Chrome)
Cross authentication... Socket.io authorization say false Authentication status is false. debug - authorized info - handshake unauthorized
通过page / browser1(Firefox)-> Rails-> Redis发送一些数据,输出:
Redis connection on application_channel Handshaked Session, authenticated user. All channels allowed. Socket.io emiting on [hLTYXuvP+13aQlIT9CZiYc1i9eg=] debug - websocket writing 5:::{"name":"hLTYXuvP+13aQlIT9CZiYc1i9eg=","args":[null]}
在浏览器2上重新加载页面(Chrome,已注销),仍在输出
在浏览器1 / Firefox上重新加载页面,并通过页面和Rails将更多数据推送到Redis,输出:
Redis connection on application_channel Handshaked Session, authenticated user. All channels allowed. Socket.io emiting on [hLTYXuvP+13aQlIT9CZiYc1i9eg=] Redis connection on application_channel Handshaked Session, authenticated user. All channels allowed. Socket.io emiting on [hLTYXuvP+13aQlIT9CZiYc1i9eg=] debug - websocket writing 5:::{"name":"hLTYXuvP+13aQlIT9CZiYc1i9eg=","args":[null]}
如您所见,事件被复制了,Redis侦听器现在捕获了两个连接。每次我重新加载page1 / Firefox时,它都会重复复制一次(3、4、5 …)。
我不了解这种行为。我想念什么?顺便说一句,你可以看到,我真的不明白或者Socket.io的目标configure,set authorization并且callback,返回真或假,如节点是伸手io.on('connection')和Redis的听众呢。
configure
set authorization
callback
io.on('connection')
好的,我知道了。我很难解决这个问题,一直在寻找时间…
这是我的代码:
io.on('connection', function(client){ console.log('Client id #'+client.id); if (client.handshake.session) { var user_socket_channel = client.handshake.session['user_socket_channel']; } var redis_listener = function(redis_channel, rawdata) { console.log('Redis connection on '+redis_channel); var data = JSON.parse(rawdata); if (data.channel) { var socket_channel = data.channel; } else { if (user_socket_channel) { var socket_channel = user_socket_channel; } else { var socket_channel = 'public' } } var rails_data = data.data; console.log('Socket.io emiting on ['+socket_channel+']'); client.emit(socket_channel, rails_data); }; redis_subs.on('message', redis_listener); client.on('disconnect', function(){ console.log('Client disconnect, removing redis listener..'); redis_subs.removeListener('message', redis_listener); }); });
根据上下文,您需要在io.client.disconnect事件上调用redis.removeListener。
这是一个可能的选择:
https://github.com/LearnBoost/Socket.IO/wiki/Configuring- Socket.IO