Answer the question
In order to leave comments, you need to log in
Duplicate messages in chat (Socket.io), what should I do?
Hello, I'm making a chat using socket.io. Solved some problems from previous questions,
but a new problem appeared.
app.get('/index', async (req, res) => {
res.sendFile(path.join(__dirname, './HTML', 'index.html'))
io.on('connection', socket => {
socket.on('Отправить сообщение', (data) => {
io.emit('Добавить сообщение', {
msg: data,
login: req.session.user.login
})
})
})
})
Answer the question
In order to leave comments, you need to log in
Solved a problem. It was necessary to create some kind of Middleware
const session = require('express-session')
const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
const sessionMiddleware = session({
secret: '...',
saveUninitialized: false,
resave: false,
store: ...
})
app.use(sessionMiddleware)
io.use((socket, next) => {
sessionMiddleware(socket.request, {}, next)
})
io.on('connect', socket => {
const session = socket.request.session
// Используем примерно так:
session.user.login
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question