我正在使用express,socket.io和socket.io-redis模块recommended in the socket.io documentation在Heroku上构建音频聊天应用程序,以在多个服务器上扩展socket.io。
对于单个测功机,连接是稳定的,但是当扩展到更大时,用户会非常频繁地断开连接。
我曾尝试使用粘性群集之类的模块来委派连接,但是that didn't work either如此,因此自此我启用了Heroku的会话亲和性来代替代理。
消息和音频被发送到正确的房间,所以我真的不确定是什么导致了不稳定的断开连接。
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const redisAdapter = require('socket.io-redis');
const { Users } = require('./utils/users.js');
const users = new Users;
const { REDIS_HOST } = process.env;
const { REDIS_PORT } = process.env;
const { REDIS_PASSWORD } = process.env;
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
io.adapter(redisAdapter({
host: REDIS_HOST,
port: REDIS_PORT,
password: REDIS_PASSWORD
}));
io.on('connection', (socket) => {
socket.on('join', (params, done) => {
const { type, name, room } = params;
socket.join(room);
users.removeUser(socket.id);
users.addUser(socket.id, type, name, room);
socket.to(room).emit('updatelistenerCount', { listenerCount });
done();
});
socket.on('disconnect', () => {
users.removeUser(socket.id);
});
});
server.listen(port, () => {
console.log(`Express server listening on port ${process.env.PORT} in ${process.env.NODE_ENV} mode.`)
});
预先感谢您的任何有用评论