如何在bluemix上configurationsocket.io粘性会话

我需要在具有多个cloudfoundry实例的IBM Bluemix上部署一个socket.io应用程序,以确保负载均衡。

我跟着socket.io文档和这个链接来使用粘性会话,并确保每个套接字转到正确的实例。

不幸的是,我的客户端连接事件刚刚断开连接,并发生错误'400xhr poll'错误。

这是我的服务器代码:

const cookieParser = require('cookie-parser') const session = require('express-session') const redis = require('redis') const app = require('express')() const http = require('http').Server(app) const io = require('socket.io')(http) const RedisStore = require('connect-redis')(session) const redisUrl = 'myredis' var session = session({ store: new RedisStore({client: client}), secret: 'mysecret', name: 'jsessionid', resave: true, saveUninitialized: true }) app.use(cookieParser) app.use(session) io.on('connection', function (socket) { console.log('a user is connected') socket.on('testPing', function () { socket.emit('testPong', {}) }) }) http.listen(process.env.PORT || 3000, function () { console.log('listening on *:' + (process.env.PORT || 3000)) }) 

经过几个小时search谷歌,我不知道该怎么试。 我真的需要一些帮助。

先谢谢你!

我最近做了这个(虽然没有Redis),要解决同样的问题; socket.io和负载平衡器不能很好地协同工作。 关键是要使用JSESSIONID cookie来获取会话亲和力。 这是我的代码:

 const expressSession = require('express-session'); 

 var app = express(); 

 app.use( expressSession({ key: 'JSESSIONID', // use a sticky session to make sockets work secret: 'arbitrary-secret', cookie: { maxAge: 24 * 60 * 60 * 1000, // 1 day secure: false }, saveUninitialized: false, resave: false }) ); //setting up socket.io for realtime communication var http = require('http').Server(app); var io = require('socket.io')(http); 

您正在使用小写jsessionid 。 尝试大写它。 会议文档在这里: https : //docs.cloudfoundry.org/concepts/http-routing.html#sessions