-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (92 loc) · 2.78 KB
/
index.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
119
120
121
122
123
124
125
126
127
128
129
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require("socket.io");
const formatMessage = require("./utils/messages");
const {
userJoin,
getCurrentUser,
userLeave,
getRoomUsers,
} = require("./utils/user");
const expressLayouts = require('express-ejs-layouts');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// const express = require("express");
const port = process.env.PORT || '3000';
// const app = express();
// const http = require('http');
// const socketio = require("socket.io");
// const server = http.createServer(app);
// const io = socketio(server);
// set up the view engine
app.set('view engine', 'ejs');
app.set('views', './views');
const botName = "ViMeet Bot";
// Run when client connects
io.on("connection", (socket) => {
socket.on("joinRoom", ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
// Welcome current user
socket.emit("message", formatMessage(botName, "Welcome to ViMeet !"));
// Broadcast when a user connects
socket.broadcast
.to(user.room)
.emit(
"message",
formatMessage(botName, `${user.username} has joined the chat`)
);
// Send users and room info
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
});
// Listen for chatMessage
socket.on("chatMessage", (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit("message", formatMessage(user.username, msg));
});
// For start Questions
socket.on("start",(question)=>{
const user = getCurrentUser(socket.id);
io.to(user.room).emit(
"message",
formatMessage(botName, `You have a new question`)
);
// Send users and room info
io.to(user.room).emit('Start_Session',question);
})
// Runs when client disconnects
socket.on("disconnect", () => {
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit(
"message",
formatMessage(botName, `${user.username} has left the chat`)
);
// Send users and room info
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
}
});
});
// using static files
app.use(express.static("assets"));
app.use(expressLayouts);
// extract style and scripts from sub pages into the layout
app.set("layout extractStyles", true);
app.set("layout extractScripts", true);
// use express router
app.use('/', require('./routes'));
server.listen(port, function (err) {
if(err) {
console.log("error in starting the server");
return;
}
console.log("server started on port ");
});