我尝试在服务器端侦听socket.io连接,但似乎客户端从未连接到服务器,因为命令行中没有显示日志消息。
这是我到目前为止的来源:
var socketIoClient = require('socket.io-client');
var client = socketIoClient('http://localhost:3000');
client.on('connect', function(socket){
console.log('Client connected.');
// Disconnect listener
socket.on('disconnect', function() {
console.log('Client disconnected.');
});
});
client.on('chat message', function(msg){
console.log(msg);
});
这是我的socket.io服务器代码:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ioSession = require('express-socket.io-session');
var session = require('express-session');
var redis = require('redis');
var redisStore = require('connect-redis')(session);
var cookieParser = require('cookie-parser');
var passport = require('passport');
var passportLocalStrategy = require('passport-local').Strategy;
var uuid = require('uuid');
var connectEnsureLogin = require('connect-ensure-login');
var bodyParser = require('body-parser');
var passportSocketIo = require('passport.socketio');
var userStore = {};
passport.use(new passportLocalStrategy(function(username, password, cb) {
//if (!user) { return cb(null, false); }
if (username != password) { return cb(null, false); }
var user = {
id: uuid.v4(),
username: username
};
userStore[user.id] = user;
return cb(null, user);
}));
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
if (!userStore.hasOwnProperty(id)) { return cb('Fehler'); }
cb(null, userStore[id]);
});
var redisClient = redis.createClient();
var sessionStore = new redisStore({
client: redisClient
});
var sessionObject = session({
secret: "my-secret",
resave: false,
saveUninitialized: false,
store: sessionStore
});
// Configure view engine to render EJS templates.
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(sessionObject);
app.use(passport.initialize());
app.use(passport.session());
app.get('/', connectEnsureLogin.ensureLoggedIn(), function(req, res) {
res.render('index', { user: req.user });
});
app.get('/login', function(req, res){
res.render('login');
});
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);
// SOCKET HANDLING
io.on('connection', function(socket){
console.log('a user connected ' + socket.handshake.session.id);
socket.on('disconnect', function(){
console.log('user disconnected ' + socket.handshake.session.id);
});
socket.on('chat message', function(msg){
var username = socket.request.user.username;
io.emit('chat message', username + ': ' + msg);
});
socket.on('login', function (data) {
console.log('login attempt with: ' + data.username + ':' + data.password);
})
});
io.use(ioSession(sessionObject, {
autoSave: true
}));
io.use(passportSocketIo.authorize({
cookieParser: cookieParser, // the same middleware you registrer in express
key: 'connect.sid', // the name of the cookie where express/connect stores its session_id
secret: 'my-secret', // the session_secret to parse the cookie
store: sessionStore, // we NEED to use a sessionstore. no memorystore please
}));
http.listen(3000, function(){
console.log('listening on *:3000');
});
我是否错过了重要的事情?