我写了一个简单的脚本(app.js)来订阅redis的频道
var app = require('express').createServer()
, io = require('socket.io').listen(app);
var redis = require("redis");
app.listen(8080);
////////////// Dev environment; replace by PERL in production use.
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
//////////////
io.sockets.on('connection', function (socket) {
socket.subscribe = redis.createClient();
socket.subscribe.subscribe('chat');
socket.subscribe.on("message", function(channel, message) {
socket.send(message);
});
socket.on('disconnect', function() {
socket.subscribe.quit();
});
socket.on('close', function() {
socket.subscribe.quit();
});
});
现在,我想让每个用户订阅私人频道成为可能。
我试图通过 socket.io.connect 传递额外的数据,如下所示:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080', {'PRIVATE_ROOM_HASH': 'secret_hash'});
socket.on('message', function (data) {
console.log(data);
});
</script>
然后在 app.js
...
io.sockets.on('connection', function (socket, args) {
socket.subscribe = redis.createClient();
socket.subscribe.subscribe(args.PRIVATE_ROOM_HASH);
socket.subscribe.on("message", function(channel, message) {
socket.send(message);
});
但它不起作用。
任何想法如何解决私人房间的问题?
谢谢。