-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (29 loc) · 918 Bytes
/
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
const express = require("express");
const http = require("http");
const { Server: SocketIOServer } = require("socket.io");
const path = require("path");
const app = express();
const port = 5000;
const server = http.createServer(app);
const io = new SocketIOServer(server);
io.on("connection", (socket) => {
console.log("User connected");
socket.on("send-loaction: ", (data) => {
io.emit("recieve-location", {id: socket.id, ...data})
})
// when disconnect user
socket.on("disconnect", () => {
io.emit("user-disconnected", { id: socket.id })
console.log("user disconnected");
});
});
// ---- integrating ejs setup ------
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.render("index");
});
server.listen(port, () => {
console.log("listening on port:", port);
});