This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (82 loc) · 2.64 KB
/
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var http = require('http');
var socket = require('socket.io');
var express = require('express');
var port = process.env.PORT || '3000';
var host = process.env.HOST || '0.0.0.0';
// Trigger express function
var app = express()
// Host the files (make public client side)
.use(express.static('src'))
.set('views', 'views')
.set('view engine', 'ejs')
.get('/', renderLogin)
.get('/room', renderRoom);
var server = http.createServer(app);
// Give server as argument
var io = socket(server);
server.listen(port, function () {
console.log('Running on:', host, port);
});
// Save for later use
var connectedUsers = {};
// Send connectedUser to client
function renderRoom(req, res) {
res.render('pages/room', {connectedUsers: connectedUsers});
}
function renderLogin(req, res) {
res.render('pages/login');
}
/* Socket.IO server-side
------------------------------ */
// Deal with connection event
io.sockets.on('connection', newConnection);
// Socket argument fire this when there is a new connection
function newConnection(socket) {
// Log the socket id
console.log('New connection: ' + socket.id);
// Emit to all other clients except user.
socket.on('CONNECT_USER', function (userProfile) {
// For every user add userProfile to object
connectedUsers[socket.id] = userProfile;
socket.broadcast.emit('CONNECT_USER', {
// Send the user profile and socket id to client
userProfile: userProfile,
id: socket.id
});
});
// When textArea received from client
socket.on('textArea', sendTextarea);
// When searchField received from client
socket.on('searchField', sendSearchfield);
// Send searchfield message to all clients
function sendSearchfield(field) {
// Api request to Youtube send it to all clients
io.emit('NEW_VIDEO',
// EncodeURI Make sure spaces work
`${(field)}`);
}
// Send textarea message to all clients
function sendTextarea(text) {
io.sockets.emit('textArea', text);
}
// Client disconnect
socket.on('disconnect', function () {
console.log('User disconnected: ' + socket.id);
// Broadcast to all other clients. Delete the socket.id from the connecter
socket.broadcast.emit('DISCONNECT_USER', socket.id);
// Delete the property
delete connectedUsers[socket.id];
});
// When video playing received from client
socket.on('videoPlay', sendPlaying);
// Send to all client except self
function sendPlaying() {
socket.broadcast.emit('videoPlay', true);
}
// When video pause received from client
socket.on('videoPause', sendPause);
// Send to all client except self
function sendPause() {
socket.broadcast.emit('videoPause', true);
}
}