-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (58 loc) · 1.89 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
const express = require('express');
const path = require('path');
const port = process.env.PORT || 3000;
const app = express();
const http = require('http');
const socketIO = require('socket.io');
// serve static assets normally
app.use(express.static(__dirname + '/dist'));
app.use(express.static('static'));
// handle every other route with index.html, which will contain
// a script tag to your application's JavaScript file(s).
app.get('/', function (request, response) {
response.sendFile(path.resolve(__dirname, 'static', 'index.html'))
});
const server = http.createServer(app);
const io = socketIO(server).listen(server);
const rooms = [{ id: "test_room", name: "Test Room", owner: "dsk07639" }];
io.on('connection', (socket) => {
console.log(socket.id + ' connected');
socket.on('disconnect', () => {
console.log(socket.id + ' disconnected')
});
socket.on('join', (data) => {
const userroom = rooms.find(room => {
return room.id === data.room;
});
if (userroom) {
socket.join(data.room);
io.to(socket.id).emit('joined', userroom);
console.log(socket.id + ' joined', userroom);
} else{
io.to(socket.id).emit('joined', false);
console.log(socket.id + ' tried to join', data.room);
}
// socket.join(rooms[0].id);
// io.to(socket.id).emit('joined', rooms[0]);
// console.log(socket.id + ' joined', rooms[0]);
});
socket.on('create', (data) => {
console.log(data['user'] + ' created room ' + socket.id + ' as ' + data['name']);
const newRoom = {
id: socket.id,
name: data['name'],
owner: data['user']
}
rooms.push(newRoom);
io.to(socket.id).emit('joined', newRoom)
});
socket.on('pause', (data) => {
io.to(data.room).emit('paused', data.state)
});
socket.on('progress', (data) => {
io.to(data.room).emit('progress-change', data.time);
})
});
server.listen(port, app.get('port'), () => {
console.log("server started on port http://localhost:" + port)
});