-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
51 lines (40 loc) · 1.42 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var express = require("express");
var session = require("express-session");
var passport = require("./config/passport");
var handlebars = require("express-handlebars");
var PORT = process.env.PORT || 8080;
var db = require("./models");
var SocketIO = require('socket.io');
var app = express();
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(session({ secret: "chit chat", resave: true, saveUninitialized: true }));
app.engine("handlebars", handlebars({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
require("./routes/apiRoutes")(app);
require("./routes/htmlRoutes")(app);
let io
db.sequelize.sync({ force: false }).then(function () {
io = SocketIO(app.listen(PORT, function () {
console.log("Server listening on: http://localhost:" + PORT);
io.on('connection', socket => {
socket.on('new-user', name => {
users[socket.id] = name
socket.broadcast.emit('user-connected', name)
})
socket.on('send-chat-message', message => {
socket.broadcast.emit('chat-message', {message: message, name: users[socket.id]
})
})
socket.on('disconnect', () => {
socket.broadcast.emit('user-disconnected', users[socket.id])
delete users[socket.id]
})
})
}));
});
//const io = require("socket.io")(4000)
const users = {}