【发布时间】:2019-09-26 06:57:27
【问题描述】:
希望你一切都好,
由于我正在创建多人游戏,所以我需要存储来自套接字的所有用户详细信息,一段时间后我需要从用户列表中创建一个组。
所以我尝试实现https://github.com/socketio/socket.io-redis 库并尝试获取所有连接的用户但它不起作用,我根据这个例子开发了Example to use socket.io-redis,但它对我不起作用,我的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>
输出
用户已在无法从 redis 获取数据的房间中加入会话,如您在屏幕截图中看到的那样
【问题讨论】:
标签: node.js express socket.io mean-stack socket.io-redis