再谈 Redis Presence

我colyseus的后端程序配置了presence。我在本地跑了2个服务A和B,一个服务监听端口2567,一个监听2568.
用户1连接了A服务并创建了一个房间id001,然后用户2连接B服务想加入id001的游戏房间发现加不成功,也查不到这个房间。
----这个是为什么呢?求大佬解答!
colyseus版本是:0.14.23 presence配置也正常,能在A服务存数据,然后在B服务获取数据。

@wuyongquan

用户2 故意连接 服务器A 的 房间 id001, 这种情况下请参考 这里.

@coco 感谢回答!我再想问下,要实现上面的功能一定要使用colyseus/proxy代理吗?同时我也发现colyseus/proxy是v0.10版本才出现的,那是否意味着v0.10版本之前上面提到的需求在colyseus内部实现的?v0.10 是拆开到proxy里实现?

@coco 在群里问了一个colyseus好友,他之前是不用colyseus/proxy模块也能实现上面提到的功能,但他用的是旧版本v0.9

@wuyongquan

你的好友大概是使用 Colyseus 提供的 MatchAPI 自己实现了与 Proxy 类似的功能.
请把好的 Colyseus 群分享给大家吧!

@COCO 您好
感谢您提供中文社区的指导,我按照您的指引 (https://github.com/colyseus/proxy) 设置了动态代理,但我发现动态代理中有3处设置端口的地方,我在前端发起请求的时候应该以哪个端口为准。我发现当我以8083发起的时候,仍然出现:
ServerError: seat reservation expired.的错误,
但如果以9001发起则请求不通:
POST http://127.0.0.1:9001/matchmake/joinOrCreate/game net::ERR_EMPTY_RESPONSE
以下是ecosystem.config.js的内容:

const os = require('os');
module.exports = {
    apps: [
         
        {
            port        : 8083,
            name        : "colyseus",
            script      : "lib/index.js", // your entrypoint file
            watch       : true,           // optional
            instances   : os.cpus().length,
            exec_mode   : 'fork',         // IMPORTANT: do not use cluster mode.
            env: {
                DEBUG: "colyseus:errors",
                NODE_ENV: "production",
            }
        },
        {
            port:9001,
            name        : "colyseus-proxy",
            script      : "./node_modules/@colyseus/proxy/bin/proxy",
            instances   : 1, // scale this up if the proxy becomes the bottleneck
            exec_mode   : 'cluster',
            env: {
                PORT:9001,
                REDIS_URL: "redis://省略...:6379/0"
            }
        }
    ]
}

您好,我修改了./node_modules/@colyseus/proxy/bin/proxy的代码,新增了日志打印,希望能有所参考,具体修改如下如下:
// query pre-existing nodes
discovery_1.getNodeList().
then(function (nodes) {
return nodes.forEach(function (node) { console.log("详情3",node,nodes);return register(node); });
}).
catch(function (err) { return console.error(err); });
发现getNodeList返回的数组中有个别端口为undefined
详情3 { processId: '2NOMTd-kb', address: '172.16.225.246:7001' } [
2|proxy | { processId: '2NOMTd-kb', address: '172.16.225.246:7001' },
2|proxy | { processId: 'N9tmo8HJP', address: '172.16.225.246:undefined' },
2|proxy | { processId: 'ezYAPhlUO', address: '172.16.225.246:7000' },
2|proxy | { processId: 'C7yXZ8SvL', address: '172.16.225.246:7003' },
2|proxy | { processId: 'rPS3NmHCA', address: '172.16.225.246:7001' },
2|proxy | { processId: '7K1i4pnko', address: '172.16.225.246:7002' },
2|proxy | { processId: 'JDwRKgWKY', address: '172.16.225.246:7002' },
2|proxy | { processId: '-LI0NAxC7', address: '172.16.225.246:7002' },
2|proxy | { processId: 'Av8G4ggJB', address: '172.16.225.246:7003' },
2|proxy | { processId: 'TxwZ5Vani', address: '172.16.225.246:undefined' },
2|proxy | { processId: 'x_ZlF1Cjc', address: '172.16.225.246:7001' },
2|proxy | { processId: 'fBRAR1jCx', address: '172.16.225.246:7000' }
2|proxy | ]
这导致了代理失败,因为端口号不能为undefined,具体报错如下:
2|proxy | Using proxy 1 /matchmake/joinOrCreate/game
2|proxy | RangeError [ERR_SOCKET_BAD_PORT]: Port should be >= 0 and < 65536. Received undefined.
2|proxy | at new NodeError (node:internal/errors:372:5)
2|proxy | at validatePort (node:internal/validators:217:11)
2|proxy | at lookupAndConnect (node:net:1035:5)
2|proxy | at Socket.connect (node:net:1011:5)
2|proxy | at Agent.connect [as createConnection] (node:net:203:17)
2|proxy | at Agent.createSocket (node:_http_agent:340:26)
2|proxy | at Agent.addRequest (node:_http_agent:291:10)
2|proxy | at new ClientRequest (node:_http_client:311:16)
2|proxy | at Object.request (node:http:96:10)
我使用mac os系统在本地进行多进程测试,我不知道这是否有影响,还是我必须找一个干净的容器。nodejs版本v16.15.1,其他包版本如下:
"@colyseus/proxy": "^0.12.8",
"@colyseus/redis-driver": "^0.14.22",
"colyseus": "^0.14.0",
"pm2": "^5.2.0",
"tsc": "^2.0.4"

您好, 请问没用proxy的时候一切正常吗?

@coco 是的,没用代理的时候,一切正常

@coco 我已经搞定了,是redis没有清除历史数据导致的, [npm install -g redis-cli,rdcli -u redisurl flushall]手动清除后问题就解决了,目前已经成功通过docker部署在阿里云的集群容器里面了,感谢您提供支持;