-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
50 lines (36 loc) · 1.24 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
const express = require('express')
const app = express();
const server = require('http').createServer(app);
const socketio = require('socket.io')
const io = socketio(server)
const cors = require('cors')
app.use(express.static(__dirname + "/public"));
// Allow requests from your client server
const corsOptions = {
origin: 'https://locationreal.onrender.com/',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
};
app.use(cors(corsOptions))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
let current_users = {}
io.on('connection', (socket) => {
console.log("A User connected! user id: "+socket.id)
socket.on('client-location', (data)=>{
current_users[socket.id] = data.username;
io.emit('server-location', {...data, id: socket.id});
})
socket.on('client-join-location', (data) => {
io.emit('client-join-server', {...data, id: socket.id})
})
socket.on('disconnect', () => {
console.log('User disconnected: ' + socket.id);
io.emit('disconnected_user', {id: socket.id, username: current_users[socket.id]})
delete current_users[socket.id]
});
})
server.listen(3000, () => {
console.log("Server is running on port 3000!")
})