我使用快递4 +护照与会议。
我想在使用socket.io
的用户之间发送私人消息在从第一个用户向第二个用户发送消息之前 - 我想验证该消息是否真的来自第一个用户。要在javascript文件中排除ID欺骗。
我尝试使用session.socket.io来获取会话和用户(https://github.com/wcamarao/session.socket.io),但它不起作用,我无法理解为什么。
"的console.log('!连接');"不起作用。似乎函数" sessionSockets.on(' connection')"未执行
require('./db');
var flash = require('connect-flash');
var mongoose = require('mongoose');
require('./config/passport');
var express = require('express');
var http = require('http');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var routes = require('./routes');
var users = require('./routes/user');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var sessionStore = new MongoStore({db:mongoose.connection.db});
var app = express();
app.use(cookieParser('keyboard cat'));
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'keyboard cat',
cookie: {httpOnly: true, secure: true, maxAge: new Date(Date.now() + 3600000)},
store: sessionStore
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
// debug('Express server listening on port ' + server.address().port);
});
var io = require('socket.io').listen(server);
var SessionSockets = require('session.socket.io-express4');
var sessionSockets = new SessionSockets(io, sessionStore, cookieParser);
sessionSockets.on('connection', function (err, socket, session) {
console.log('connect!');
socket.on('sendMessageToUser', function(data){
console.log(data);
console.log('User is ' + req);
});
//now you can read session.foo from your express routes or connect middlewares
});