【问题标题】:unable to get data from socket.io-redis with nodejs无法使用 nodejs 从 socket.io-redis 获取数据
【发布时间】: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 获取数据的房间中加入会话,如您在屏幕截图中看到的那样

Output of Above Code

【问题讨论】:

    标签: node.js express socket.io mean-stack socket.io-redis


    【解决方案1】:

    我测试了你的代码,它对我有用,可能是 redis 的错误,你的回调有什么错误吗:

    your_namespace_socket.adapter.clients([room], (err, clients) => {
        if(err) console.log(err)
        console.log(clients);
    });
    

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2017-08-31
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多