-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (71 loc) · 2.26 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
// for change the path
const Path = require("path");
const express = require("express");
// for create node srver
const http = require("http");
// for comunicate between client and server
const socketio = require("socket.io");
// for find bad words
const filterMessage = require("bad-words");
const messagefunc = require("./src/messagefunc");
const { adduser, removeuser, getuser, getuserinroom } = require("./src/user");
const app = express();
app.use(express.static(Path.join(__dirname, "./public")));
const server = http.createServer(app);
const io = socketio(server);
// socket is only run a pharticuler connection
io.on("connection", (socket) => {
socket.on("join", (options, callback) => {
const { error, user } = adduser({ id: socket.id, ...options });
// console.log(error);
if (error) {
return callback(error);
}
socket.join(user.room);
// to send one one user
socket
.to(user.room)
.emit("message", messagefunc(`welcome ${user.username}`));
// to send message when new use joined
socket.broadcast
.to(user.room)
.emit("message", messagefunc(`${user.username} has joined`));
callback();
});
socket.on("sendMessage", (message, callback) => {
const user = getuser(socket.id);
const filter = new filterMessage();
if (filter.isProfane(message)) {
return callback("Bad word not Allow!!");
}
// to send all the user
io.to(user.room).emit("message", messagefunc(user.username, message));
callback("Delivere");
});
socket.on("send-location", ({ latitude, longitude }, callback) => {
const user = getuser(socket.id);
io.to(user.room).emit(
"sharelocation",
messagefunc(
user.username,
`http://www.google.com/maps?q=${latitude},${longitude}`
)
);
callback();
});
socket.on("disconnect", () => {
const user = removeuser(socket.id);
console.log(user);
if (user) {
io.to(user.room).emit(
"message",
messagefunc(`${user.username} has been disconncted`)
);
}
});
});
// if we deploy this app on heroku then heroku take port in process.env.PORT if we run this on locally then its take 3000
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`App Listen on ${PORT}`);
});