-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
70 lines (56 loc) · 1.74 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
import dotenv from "dotenv/config";
import express from "express";
import Router from "./routes/route.js";
import cors from "cors";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import { Connection } from "./database/db.js";
import { Server } from "socket.io";
const app = express();
app.use(
cors({
origin: ["http://localhost:3000"],
credentials: true,
})
);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use("/", Router);
const PORT = process.env.PORT || 8000;
app.get("/", (req, res) => {
res.send("Hello");
});
const server = app.listen(PORT, () => {
console.log(`listening to port ${PORT}!`);
});
Connection();
/////////////////////////////////////////////////////////////////////////////////
const io = new Server(server,{
pingTimeout: 60000,
cors: {
origin: ["http://localhost:3000"],
// credentials: true,
},
});
io.on("connection", (socket) => {
console.log("someone is connected");
// console.log(`${socket.handshake.query.name} joined room : ${socket.handshake.query.id}`);
// socket.join(socket.handshake.query.id);
socket.on("setup" , ({sender}) => {
// console.log(sender._id);
socket.join(sender._id);
console.log(`${sender.name} joined room : ${sender._id}`);
})
socket.on("sendNotification", ({sender , receiver , type}) => {
// console.log(receiver)
socket.in(receiver._id).emit("getNotification", {sender: sender , type: type});
})
socket.on("send message",({content,id,sender}) => {
// console.log(content,id);
socket.in(id).emit("new message",{content:content , sender:sender});
})
socket.on("disconnect", () => {
console.log("disconnected");
});
});