我想合併express.js和socket.io會話。下面是我的代碼(socket.io部分)合併socket.io和express.js會話
var io = require('socket.io').listen(app);
io.set('log level', 1);
io.sockets.on('connection', function (socket) {
console.log('client connected');
client.send(client.id);//send client id to client itself
socket.on('connect', function(){
console.log(socket.id + ' connected');
});
socket.on('disconnect', function(){
console.log(socket.id + ' disconnected');
});
});
我express.js會話設置:
app.configure(function() {
//app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/static'));
app.use(express.cookieParser());
app.use(express.session({store: MemStore({
reapInterval: 60000 * 10
}), secret:'foobar', key: 'express.sid'
}));
我的主要問題是在我的終端,當用戶從一個URL到另一個,會話ID傳播也有變化:但我不希望它被改變。
info - socket.io started
client connected
client connected
4Z0bYHzfWCEFzbbe4WUK disconnected
e_uSvxhSLbLAC9-F4WUL disconnected
client connected
bKDy90gsrWWTRJDD4WUM disconnected
client connected
RJ5qqCL2wfmXbd7U4WUN disconnected
client connected
wjN5Sqx4rucRtWL_4WUO disconnected
連接在您的示例中未定義。任何想法解決? – Yagiz 2012-08-06 14:44:37
@Alex如果你克隆了''lolbr'',確保你在它下面安裝了'express'。只需克隆,然後輸入''cd''並鍵入''npm install''。這應該工作。 – Fabian 2012-08-06 14:48:40
因此,parseSignedCookies現在不可用。你如何建議重新編寫代碼?我不熟悉socket.io中的Cookies,但我發現「var cookies = cookie.parse('foo = bar; cat = meow; dog = ruff');」例。我應該如何實現這個例子? – Yagiz 2012-08-06 15:22:08