general chat
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const chatService = require('./global_chat');
|
||||
|
||||
function setupSocketIO(io)
|
||||
{
|
||||
io.use((socket, next) =>
|
||||
{
|
||||
const token = socket.handshake.auth.token;
|
||||
if (!token)
|
||||
return (next(new Error('Authentication error')));
|
||||
|
||||
try
|
||||
{
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
socket.user = decoded;
|
||||
next();
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
next(new Error('Authentication error'));
|
||||
}
|
||||
});
|
||||
|
||||
io.on('connection', (socket) =>
|
||||
{
|
||||
console.log(`User connected: ${socket.user.username}`);
|
||||
|
||||
socket.join('general-chat');
|
||||
socket.on('chat-message', async(data) =>
|
||||
{
|
||||
const message = await chatService.saveMessage(socket.user.userId, data.content);
|
||||
io.to('general-chat').emit('chat-message',
|
||||
{
|
||||
id:message.id,
|
||||
username: socket.user.username,
|
||||
content: message.content,
|
||||
created_at: message.created_at
|
||||
});
|
||||
});
|
||||
socket.on('disconnect', () =>
|
||||
{
|
||||
console.log(`User disconnected: ${socket.user.username}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = setupSocketIO;
|
||||
Reference in New Issue
Block a user