-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (65 loc) · 2.25 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
69
70
71
72
73
74
75
76
77
78
79
80
81
// this is a core example of using socket io with express
// require express and instaciate it
const express = require('express');
const app = express();
// instaciating http server with express
const server = require('http').Server(app);
const io = require('socket.io')(server);
const PORT = process.env.PORT || 8080;
const path = require('path');
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './views/index.html'));
});
server.listen(PORT, () => {
console.log(`server on ${PORT}`);
})
const users = [];
// socket io connection process
// this is what happens when a user connects, a connection event happens
// so we establish a connection listener.
io.on('connection', function (socket) {
console.log('new user connected: id -> ', socket.id);
users.push(socket);
console.log('users connected =', users.length);
// socket.emit('news', { hello: 'world' });
// socket.on('my other event', function (data) {
// console.log(data);
// });
socket.on('paused', (data) => {
console.log('video paused somewhere, emitting to all', data);
socket.broadcast.emit('pause', {status: "pausing"});
})
socket.on('ready', (data) => {
console.log('video ready to play');
});
socket.on('play', (data) => {
console.log('playing video somewhere, emiting to all', data);
socket.broadcast.emit('play', {status: "playing"});
});
// this is gonna be the long switch statement
socket.on('statuschange', (data) => {
console.log('status change', data);
});
socket.on('newVideo', (data) => {
socket.broadcast.emit('newVideo', {urlid: `${data.urlid}`})
console.log('new video broadcasted to everyone');
})
socket.on('disconnect', (data) => {
users.pop();
console.log('user disconnected, remaining =', users.length);
});
});
// DONE var app = require('express')();
// DONE var server = require('http').Server(app);
// DONE var io = require('socket.io')(server);
// DONE server.listen(80);
// DONE app.get('/', function (req, res) {
// res.sendfile(__dirname + '/index.html');
// });
// DONE io.on('connection', function (socket) {
// socket.emit('news', { hello: 'world' });
// socket.on('my other event', function (data) {
// console.log(data);
// });
// });