-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (41 loc) · 1.41 KB
/
server.js
File metadata and controls
51 lines (41 loc) · 1.41 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const path = require('path');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/track', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'track.html'));
});
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('join-session', (sessionId) => {
socket.join(sessionId);
console.log(`User joined session: ${sessionId}`);
});
socket.on('client-connect', (sessionId) => {
socket.join(sessionId);
console.log(`Client connected to session: ${sessionId}`);
io.to(sessionId).emit('client-status', 'connected');
});
socket.on('send-location', (data) => {
const { sessionId, latitude, longitude } = data;
io.to(sessionId).emit('receive-location', { latitude, longitude });
});
socket.on('send-frame', (data) => {
const { sessionId, image } = data;
io.to(sessionId).emit('receive-frame', image);
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`listening on *:${PORT}`);
});