others - node.js - 无法使用nodejs从socket.io-redis获取数据

我试图实现https://github.com/socketio/socket.io-redis库,并且试图获得所有已连接的用户,但是它没有工作。

index.js


var express = require('express');


var app = express();


var server = require('http').Server(app);


var io = require('socket.io')(server);


var redis = require('socket.io-redis');


io.adapter(redis({ host: 'localhost', port: 6379 }));


var your_namespace_socket = io.of('/');



app.get('/', function(req, res) {


 res.sendfile('index.html');


});


your_namespace_socket.on('connection', function(socket) {



 console.log(socket.id);


 socket.on('join', function(room) {



 socket.join(room);


 console.log(room);



 //log other socket.io-id's in the room


 your_namespace_socket.adapter.clients([room], (err, clients) => {


 console.log(clients);


 });


 });


});


server.listen(3000, function() {


 console.log('listening on *:3000');


});



index.html


<html>



<head>


 <title>Hello world</title>


</head>


<script src="/socket.io/socket.io.js"></script>


<script>


var socket = io('/');


socket.emit('join', 'testingroom');


</script>



<body></body>



</html>



时间:

我测试了你的代码,我这儿可以工作,可能在回调中有错误?


your_namespace_socket.adapter.clients([room], (err, clients) => {


 if(err) console.log(err)


 console.log(clients);


});



...