-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
101 lines (86 loc) · 2.75 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const path = require("path");
const users = require("./routes/api/users");
const app = express();
const server = require("http").createServer(app);
const passport = require("passport");
const io = require("socket.io")(server);
const items = require("./routes/api/items");
const chats = require("./routes/api/chats");
// const socketItems = require("./routes/api/items")(io)
app.use(bodyParser.json());
// DB config
const db = require("./config/keys").mongoURI;
// Connect to Mongo
mongoose
.connect(
db,
{
useNewUrlParser: true
}
)
.then(() => console.log("MongoDb connected ..."))
.catch(err => console.log(err));
app.use("/api/items", items);
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);
// Routes
app.use("/api/users", users);
//Chats route
app.use("/api/chats", chats)
//Serve static assets if in production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// const io = require("socket.io").listen(server);
const port = process.env.PORT || 5000;
// const port = 5000;
server.listen(port, () => console.log(`Server started on port ${port}`));
//Socket IO
// io.configure(() => {as
// io.set("transports", ["xhr-polling"]);
// io.set("polling duration", 10);
// });
io.on("connection", client => {
client.on("update", data => {
io.sockets.emit("update", { message: data.message });
});
client.on("delete", data => {
io.sockets.emit("delete", { message: data.message });
});
client.on("disconnect", function() {
console.log("disconnect __________________");
});
client.on("privateChat", data => {
// console.log(client.id);
// client.join(data.room);
// console.log(io.nsps);
// console.log("heree", data.room);
io.sockets.emit(`${data.room}_update`, { message: data.message });
// privateConnection(data)
// const nsp = io.of(`/${data.room}`);
// nsp.emit('hi', 'Hello everyone!');
// io.in(`privateChat`).emit(`${data.room}`, {msg: 'hello'});
// io.in(`${data.room}`).emit(`${data.room}`, {message: data.message})
});
client.on("deletePrivate", data => {
// console.log(data.room);
io.sockets.emit(`${data.room}_delete`, { message: data.message });
});
});
function privateConnection(data){
// console.log(data, "inside private");
const nsp = io.of(`/${data.room}`);
nsp.on('connection', function(socket) {
// console.log('someone connected');
nsp.emit(`${data.room}`, {message: data.message});
});
}
// io.listen(port);
console.log("listening on port ", port);