-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (45 loc) · 1.36 KB
/
Copy pathserver.js
File metadata and controls
56 lines (45 loc) · 1.36 KB
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
const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const cors = require("cors");
const { json } = require("body-parser");
const mongoose = require("mongoose");
const app = express();
const PORT = process.env.PORT || 5000;
require("dotenv").config();
app.use(json());
app.use(cors());
app.use(require("./routes"));
mongoose.connect(process.env.MONGOOSE_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection.once("open", () => {
console.log("Connected to mongoDB");
});
const server = http.createServer(app);
const io = socketIO(server, {
cors: {
origin: ["http://localhost:3000", "https://ptthanh0606.github.io"],
methods: ["GET", "POST", "PUT", "DELETE"],
},
});
io.on("connection", (socket) => {
console.log(`User ${socket.id} connected to socket`);
let conversationIdRoom = "";
socket.on("OPEN_CONVERSATION", ({ conversationId }) => {
socket.join(conversationId);
conversationIdRoom = conversationId;
});
socket.on("CLIENT_MESSAGE_SEND", (data) => {
console.log(data, conversationIdRoom);
io.to(conversationIdRoom).emit("MESSAGE_INCOMMING", data);
});
socket.on("disconnect", () => {
console.log("User disconnected", socket.id);
});
});
server.listen(PORT, () => {
console.log("App is listening on port " + PORT);
});
module.exports = app;