-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (103 loc) · 3.28 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
const express = require("express");
const socketio = require("socket.io");
const http = require("http");
const morgon = require("morgan");
const cors = require("cors");
const app = express();
const path = require("path");
const server = http.createServer(app);
const io = socketio(server);
const { addUser, removeUser, getUser, getUsersInRoom } = require("./users");
app.use(cors());
app.use(morgon("dev"));
//? server static flies
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
//! Chat service
io.on("connection", (socket) => {
//console.log(socket.id);
//* socket deals as cuurent user
//* io use for global
socket.on("join", (data, callback) => {
const { error, user } = addUser({
id: socket.id,
name: data.name,
room: data.room,
});
console.log(user, "join");
if (error) {
console.log({ error });
return callback({ error });
}
socket.emit("message", {
user: "Admin",
text: `${user.name} welcome to the room`,
}); //? send message event to current user only
socket.broadcast.to(user.room).emit("message", {
user: "Admin",
text: `${user.name} has joined the room`,
}); //? send message event to other user but not current user
socket.join(user.room);
io.to(user.room).emit("roomData", {
room: user.room,
users: getUsersInRoom(user.room),
});
callback();
});
//? receive message from client
socket.on("sendMessage", (message, callback) => {
const user = getUser(socket.id);
// console.log(user);
io.to(user.room).emit("message", { user: user.name, text: message });
io.to(user.room).emit("roomData", {
room: user.room,
users: getUsersInRoom(user.room),
});
callback();
});
//! handle video call
//? send caller offer to receiver
socket.on("sendOffer", ({ signal, receiver, caller }) => {
console.log("sending to: ", receiver.name, " :from: ", caller.name);
io.to(receiver.id).emit("offer", { signal, receiver, caller });
});
//? send receiver anwser back to caller
socket.on("sendAnswer", ({ signal, receiver, caller }) => {
console.log("sending back to: ", caller.name, " :from: ", receiver.name);
io.to(caller.id).emit("answer", { signal, receiver, caller });
});
//? send ice-candidate to receiver
socket.on("send-ice-candidate-to-receiver", (payload) => {
io.to(payload.receiver.id).emit(
"ice-candidate-from-caller",
payload.icecandidate
);
});
//? send ice-candidate to caller
socket.on("send-ice-candidate-to-caller", (payload) => {
io.to(payload.caller.id).emit(
"ice-candidate-from-receiver",
payload.icecandidate
);
});
//! handle user left
socket.on("disconnect", () => {
console.log("disconnect...");
const user = removeUser(socket.id);
if (user) {
io.to(user.room).emit("message", {
user: "Admin",
text: `${user.name} has left.`,
});
io.to(user.room).emit("left", { id: user.id });
io.to(user.room).emit("roomData", {
room: user.room,
users: getUsersInRoom(user.room),
});
}
});
});
const port = process.env.PORT || 8000;
server.listen(port, () => console.log(`Server running at ${port}`));