我需要与nodejs进行会话,但是使用socket.io ..
我有这个功能代码,它与get方法一起使用
var express = require('express')
var session = require('express-session')
var app = express()
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 28800000 }}))
app.get('/', function(req, res, next) {
var sess = req.session
if (sess.views) {
sess.views++
res.setHeader('Content-Type', 'text/html')
res.write('<p>views: ' + sess.views + '</p>')
res.end()
console.log(sess.views)
} else {
sess.views = 1
res.end('welcome to the session demo. refresh!')
console.log(sess.views)
}
})
console.log('Server iniciado y operando!')
app.listen(3000)
&#13;
但我需要使用此方法的会话
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(express.static('client'));
var messages = [{
id: 1,
text: 'Bienvenido al chat privado de Socket y Node de Luis Zurita',
nickname: 'BELICOFSA dice:'
}];
io.on('connection', function(socket){
console.log("El equipo con IP: "+socket.handshake.address+" se ha conectado...");
socket.emit('caja-chat', messages);
socket.on('add-message', function(data){
messages.push(data);
io.sockets.emit('caja-chat', messages);
});
});
server.listen(3000, function(){
console.log('Servidor esta Funcionando en http://localhost:6677');
});
&#13;
我最擅长的是这个,但它不能作为代码1 你能救我吗?
var express = require('express')
var app = express()
var server = require('http').Server(app);
var ios = require('socket.io-express-session');
var io = require('socket.io')(server);
var Session = require('express-session');
var session = Session({
secret: 'sdfgf',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 100000 }
});
var messages = [{
id: 1,
text: 'Bienvenido al chat privado de Socket y Node de Luis Zurita',
nickname: 'BELICOFSA dice:'
}];
server.listen(3000, function(){
console.log('Servidor esta Funcionando en http://localhost:6677');
});
io.use(ios(session));
app.use(express.static('client'));
io.on('connection', function(socket){
socket.handshake.session.save;
var sess = socket.handshake.session;
if (sess.views) {
sess.views++
} else {
sess.views = 1
}
console.log(sess);
socket.emit('caja-chat', messages);
socket.on('add-message', function(data){
messages.push(data);
io.sockets.emit('caja-chat', messages);
});
});
&#13;