很想做一个简单的聊天系统,为此我必须展示 所有的客户都是连接的。我尝试使用sivrel技术而且它不起作用。 我试图在客户端连接时发出一个套接字并且它不起作用。 这就是我的尝试:
app.post('/login', (req, res)=>{
let user = req.body.username
let pass = req.body.password
console.log("le user est = " + user + " \nle password est = " + pass);
connection.query('SELECT * FROM user WHERE username = ? AND password = ?', [user,pass], function (error, results, fields) {
if (error) throw error;
console.log(results.length);
console.log(results[0]);
if (results.length==1){
const user_id = results[0]
io.sockets.emit('new_user',{user:results[1]})
req.login(user_id,(err)=>{
res.redirect('/home');
})
}else {
res.redirect('/');
}
});
});
那个完整的代码: server.js:
let express = require('express');
let app = express();
let bodyParser = require('body-parser')
let server = require('http').createServer(app);
var io = require('socket.io')(server);
let session = require('express-session')
let MySQLStore = require('express-mysql-session')(session);
let passport = require('passport');
let mysql = require('mysql');
let hbs = require('hbs')
var options = {
host: 'localhost',
user: 'root',
password: '',
database: 'robot'
};
var sessionStore = new MySQLStore(options);
let connection = mysql.createConnection(options);
connection.connect(function(err) {
if (err) {
console.error('MYSQL error connecting : ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
app.use(session({
secret: 'ldifgquoqjfejg',
store: sessionStore,
//comme elle change que lorsque on modéfie la session
resave: false,
saveUninitialized: false,
//cookie: { secure: true }
}))
app.use(passport.initialize());
app.use(passport.session());
//hbs configure
app.set('view engine', 'html');
app.engine('html', hbs.__express);
hbs.registerPartials(__dirname + '/views/partials');
hbs.localsAsTemplateData(app);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', (req, res)=>{
res.render('login');
});
app.get('/home', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('index');
});
app.get('/robot', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('robot');
});
app.get('/userlist', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('listeuser');
});
app.get('/history', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('history');
});
app.get('/chat', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('chat');
});
app.get('/cam', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('cam');
});
app.get('/bras', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('bras');
});
app.get('/base', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('base');
});
app.get('/ajout', authenticationMiddleware(), (req, res)=>{
app.locals.user = req.user.username
res.render('ajout');
});
app.get('/Logout', (req, res)=>{
req.logout();
req.session.destroy();
res.redirect('/');
});
app.post('/login', (req, res)=>{
let user = req.body.username
let pass = req.body.password
console.log("le user est = " + user + " \nle password est = " + pass);
connection.query('SELECT * FROM user WHERE username = ? AND password = ?', [user,pass], function (error, results, fields) {
if (error) throw error;
console.log(results.length);
console.log(results[0]);
if (results.length==1){
const user_id = results[0]
io.sockets.emit('new_user',{user:results[1]})
req.login(user_id,(err)=>{
res.redirect('/home');
})
}else {
res.redirect('/');
}
});
});
passport.serializeUser(function(user_id, done) {
done(null, user_id);
});
passport.deserializeUser(function(user_id, done) {
done(null, user_id);
});
//socket
io.on('connection', (socket)=>{
socket.emit('user','data')
});
//socket
server.listen(8000);
app.use(express.static('./views'));
function authenticationMiddleware(){
return(req, res, next)=>{
//console.log('req.session.passport.user: ${JSON.stringify(req.session.passport)}')
if (req.isAuthenticated()) return next();
res.redirect('/')
}
}
和chat.html
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
socket = io.connect('http://localhost:8000')
socket.on('new_user',(data)=>{
document.getElementById('ilyes').innerHTML=data.user
})
</script>