gpt4 book ai didi

node.js - 'adapter' 中没有方法 'io'(node.js、heroku、RedisCloud、socket.io)

转载 作者:可可西里 更新时间:2023-11-01 11:21:42 25 4
gpt4 key购买 nike

在 Heroku 上使用 RedisCloud 和 node.js 允许( future )扩展到多个 dyno。

按照以下步骤让 Redis 工作: https://devcenter.heroku.com/articles/rediscloud#using-redis-from-node-js

例如,下面带有注释“Prints 'bar'”的行确实​​将“bar”写入控制台。

然后按照此添加用于缩放的 socket.io-redis: https://github.com/Automattic/socket.io-redis

上面应该允许我使用类似下面的东西:

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

我下面的代码中的等价物是:

io.adapter(ioredis (redisOptions));

但是,我一直收到错误消息说 io 没有方法“适配器”。

或者,如果我首先使用“if”语句检查 io.adapter 是否存在(如下面的代码所示),则条件代码永远不会执行。

谁能看出我在这里做错了什么?为什么 io.adapter 不存在?

在 package.json 中

 "dependencies": {
"redis": "^0.12.1",
"express": "^3.4.8",
"socket.io": "^0.9.16",
"socket.io-redis":"^0.1.4"
}

还尝试将其添加到 package.json

"socket.io-adapter": "^0.2.0"

在 app.js(我的 Node 服务器代码)中:

var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, redis = require('redis')
, ioredis = require('socket.io-redis')
, url = require('url')
, redisURL = url.parse(process.env.REDISCLOUD_URL);

然后在 app.js 中:

io.sockets.on('connection', function (socket) {
var pub1 = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
var sub1 = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
pub1.auth(redisURL.auth.split(":")[1]);
sub1.auth(redisURL.auth.split(":")[1]);

var redisOptions = {
pubClient: pub1,
subClient: sub1,
host: redisURL.hostname,
port: redisURL.port
};
pub1.set('foo', 'bar');
sub1.get('foo', function (err, reply) {
console.log("redis test : "+reply.toString());
// Prints 'bar'
});
if (io.adapter) {
// Never reached
io.adapter(ioredis (redisOptions));
console.log ("mylog: io.adapter found");
}
console.log ("mylog server connection event fired");

最佳答案

socket.io-redis 旨在与 socket.io 1.x 一起使用。

0.9.x 版本没有适配器。

关于node.js - 'adapter' 中没有方法 'io'(node.js、heroku、RedisCloud、socket.io),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27560364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com