This repository has been archived on 2026-05-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
chatless/backend/service-message/server.js
2021-05-29 00:05:00 +02:00

90 lines
No EOL
2.4 KiB
JavaScript

const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const cors = require ('cors');
const cookieParser = require('cookie-parser');
const auth = require ('./auth');
const bodyParser = require ('body-parser');
const messages = require('./mongodb-message');
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "http://127.0.0.1:4200",
methods: ["GET", "POST"],
credentials: true
}
});
const port = process.env.PORT || 3001;
app.use(bodyParser.json());
app.use(cors({origin: 'http://127.0.0.1:4200', credentials: true}));
app.use(cookieParser());
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection',socket => {
let users = {};
const session = auth.getSession(socket.request);
const getUsername = auth.getUsername(session);
if (getUsername === -1) {
socket.emit('error','not authenticated');
}
console.log(`${getUsername} joined the chat.`);
socket.broadcast.emit('general',[{
username: 'Server',
date: new Date(),
channel: 'general',
message: `${getUsername} joined the chat.`
}]);
users[socket.id] = getUsername;
messages.find({}, {'_id':0},{sort: {'date':1}},(err, res) => {
if(err) throw err;
if(res.length > 0){
//console.log(res, res.length);
socket.emit('general',res);
}
socket.emit('general',[{
username: 'Server',
date: new Date(),
channel: 'general',
message: `${getUsername} joined the chat.`
}]);
});
socket.on('general',function(data){
const username = data.username;
const date = Date.now();
const channel = 'general';
const message = data.message;
messages.insertMany([{
username: username,
date: date,
channel: channel,
message: message
}
]).then(function(){
console.log(data, "inserted");
socket.broadcast.emit('general',[data]);
socket.emit('general',[data]);
}).catch(function(error){
console.log("error",error);
});
});
socket.on("disconnect", function() {
console.log(`${getUsername} left the chat.`);
});
});
server.listen(port, () => {
console.log(`listening on *:${port}/`);
});