forked from Nick-Giannini/Conversa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
118 lines (90 loc) · 3.15 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const express = require('express');
const session = require('express-session');
const routes = require('./controllers');
const exphbs = require('express-handlebars');
const path = require('path');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const { Messages, Room } = require('./models/');
const sequelize = require('./config/connection');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const PORT = process.env.PORT || 3001;
//use .env because we don't know what port the server will use
const sess = {
secret: 'Super secret secret', //in industry you will use uuid to generate a random key
cookie: {},// use max and min for when user logs out?
resave: false,
saveUninitialized: true,
store: new SequelizeStore({
db: sequelize
})
};
app.use(session(sess));
const hbs = exphbs.create();
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(routes);
var usernames = {};
var rooms = [
{ name: "global", creator: "Anonymous" },
{ name: "chess", creator: "Anonymous" },
];
io.on("connection", function (socket) {
socket.on("createUser", async function (username) {
socket.username = username;
usernames[username] = username;
socket.currentRoom = "global";
socket.join("global");
socket.emit("updateChat", "INFO", "You have joined global room");
socket.broadcast
.to("global")
.emit("updateChat", "INFO", username + " has joined global room");
io.sockets.emit("updateUsers", usernames);
const updatedRooms = await Room.findAll({ raw: true });
socket.emit("updateRooms", updatedRooms, "global");
});
socket.on("sendMessage", function (data) {
io.sockets.to(socket.currentRoom).emit("updateChat", socket.username, data);
});
socket.on("createRoom", async function (room) {
if (room != null) {
rooms.push({ name: room, description: socket.username });
await Room.create({ name: room, description: socket.username })
const updatedRooms = await Room.findAll({ raw: true });
io.sockets.emit("updateRooms", updatedRooms, null);
}
});
socket.on("updateRooms", function (room) {
socket.broadcast
.to(socket.currentRoom)
.emit("updateChat", "INFO", socket.username + " left room");
socket.leave(socket.currentRoom);
socket.currentRoom = room;
socket.join(room);
// emit message updating the room
socket.emit("updateChat", "INFO", "You have joined " + room + " room");
socket.broadcast
.to(room)
.emit(
"updateChat",
"INFO",
socket.username + " has joined " + room + " room"
);
});
socket.on("disconnect", function () {
delete usernames[socket.username];
io.sockets.emit("updateUsers", usernames);
socket.broadcast.emit(
"updateChat",
"INFO",
socket.username + " has disconnected"
);
});
});
sequelize.sync({ force: false }).then(() => {
http.listen(PORT, () => console.log(`Now listening http://localhost:${PORT}/`));
});