-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
33 lines (27 loc) · 802 Bytes
/
app.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
const express = require("express");
const app = express();
const server = require("http").Server(app);
var io = require("socket.io")(server);
const path = require("path");
PORT = process.env.PORT || 5000;
let locationMap = new Map();
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.send("Hello World!");
});
io.on("connection", socket => {
socket.on("updateLocation", pos => {
locationMap.set(socket.id, pos);
console.log(socket.id)
});
socket.on("getLocations", () => {
socket.emit("currentLocations", Array.from(locationMap));
});
socket.on("disconnect", () => {
console.log("disconnected",socket.id)
locationMap.delete(socket.id);
});
});
server.listen(PORT, () => {
console.log(`Server started on ${PORT}`);
});