gpt4 book ai didi

node.js - openshift 上的 socket.io redis 存储

转载 作者:可可西里 更新时间:2023-11-01 11:00:55 24 4
gpt4 key购买 nike

我正在尝试在 node.js 上设置 socket.io 以使用 redisstore,以便我可以与 pubsub 与 opeshift 平台上的多个 Node 进行通信,但我无法连接到 redis 服务器。我正在使用 this cartridge .我试着连接

var pub    = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT,
process.env.OPENSHIFT_REDIS_DB_HOST);

但它不起作用(我发现了原因:createClient() only accept IP addresses)并且它回退到默认端口和主机,然后我运行了rhc port-forward:

$ rhc port-forward appname
Checking available ports ... done
Forwarding ports ...
Address already in use - bind(2) while forwarding port 8080. Trying local port 8081

To connect to a service running on OpenShift, use the Local address

Service Local OpenShift
--------------- --------------- ---- ----------------------------------------------
haproxy 127.0.0.1:8080 => 127.5.149.130:8080
haproxy 127.0.0.1:8081 => 127.5.149.131:8080
s_redis_db_host 127.0.0.1:54151 => blabla.appname.rhcloud.com:54151

Press CTRL-C to terminate port forwarding

所以我认为我做错了,我不得不像这样设置端口:

var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT);

但我得到的只是这个

info: socket.io started
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:54151 failed - connect ECONNREFUSED
at RedisClient.on_error (/var/lib/openshift/532c3790e0b8cd9bb000006b/app-root/runtime/repo/node_modules/socket.io/node_modules/redis/index.js:149:24)
at Socket.<anonymous> (/var/lib/openshift/532c3790e0b8cd9bb000006b/app-root/runtime/repo/node_modules/socket.io/node_modules/redis/index.js:83:14)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:426:14
at process._tickCallback (node.js:415:13)
DEBUG: Program node server.js exited with code 8

我尝试连接

telnet $OPENSHIFT_REDIS_DB_HOST $OPENSHIFT_REDIS_DB_PORT

而且效果很好...您有什么建议吗?我做错了吗? (我还是 redis 和 socket.io 的新手)(我省略了其余代码,因为我知道它可以工作,我在本地机器上没有问题,我只是无法连接...)非常感谢

最佳答案

but it doesn't work (and I found out why: createClient() only accept IP addresses) and it fallback to the default port and host

它确实支持主机, createClient 使用 net.createConnection(port, host);确实支持主机名。

以下代码将帮助您找到问题:

console.log(process.env);
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT,
process.env.OPENSHIFT_REDIS_DB_HOST, {auth_pass: process.env.OPENSHIFT_REDIS_DB_PASSWORD});
pub.on('error', console.log.bind(console));
pub.on('ready', console.log.bind(console, 'redis ready'));

您的 openshift Redis 实例是否需要 AUTH

关于node.js - openshift 上的 socket.io redis 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562849/

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