-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (132 loc) · 5.18 KB
/
server.js
File metadata and controls
158 lines (132 loc) · 5.18 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Load required modules
var http = require("http"); // http server core module
var express = require("express"); // web framework external module
var serveStatic = require('serve-static'); // serve static files
var socketIo = require("socket.io"); // web socket external module
var easyrtc = require("easyrtc"); // EasyRTC external module
require('dotenv').config() // Global env variables
var session = require("express-session"); // Express sessions handling
var passport = require("passport"); // Authentication middleware
var SpotifyStrategy = require('passport-spotify').Strategy; // Spotify strategy for Passport
// Set process name
process.title = "node-easyrtc";
// Get port or default to 8080
var port = process.env.PORT || 8080;
// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
var app = express();
app.use(express.static('public'));
// Passport and Session Config
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/auth/spotify');
}
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(session({
secret: process.env.cookieSecret || "top99secret42cookie",
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1.21e+9 }
}));
app.use( function(req, res, next) {
res.locals.currentUser = req.user;
next();
});
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(
new SpotifyStrategy(
{
clientID: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_SECRET,
callbackURL: 'http://localhost:8080/auth/spotify/callback',
scope: ["user-modify-playback-state", "streaming"]
},
function(accessToken, refreshToken, expires_in, profile, done) {
process.nextTick(function() {
return done(null, {profile, accessToken});
});
})
);
app.use(passport.initialize());
app.use(passport.session());
app.get('/', ensureAuthenticated, function(req, res) {
res.render('index.ejs', { user: req.user });
});
app.get('/auth/spotify', passport.authenticate('spotify', function(req, res) {
// The request will be redirected to spotify for authentication, so this
// function will not be called.
})
);
app.get('/auth/spotify/callback', passport.authenticate('spotify', {
failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
}
);
app.get('/login', function(req, res) {
res.render('login.ejs', { user: req.user });
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// Start Express http server
var webServer = http.createServer(app);
// Start Socket.io so it attaches itself to Express server
var socketServer = socketIo.listen(webServer, {"log level":1});
var myIceServers = [
{"url":"stun:stun.l.google.com:19302"},
{"url":"stun:stun1.l.google.com:19302"},
{"url":"stun:stun2.l.google.com:19302"},
{"url":"stun:stun3.l.google.com:19302"}
// {
// "url":"turn:[ADDRESS]:[PORT]",
// "username":"[USERNAME]",
// "credential":"[CREDENTIAL]"
// },
// {
// "url":"turn:[ADDRESS]:[PORT][?transport=tcp]",
// "username":"[USERNAME]",
// "credential":"[CREDENTIAL]"
// }
];
easyrtc.setOption("appIceServers", myIceServers);
easyrtc.setOption("logLevel", "debug");
easyrtc.setOption("demosEnable", false);
// Overriding the default easyrtcAuth listener, only so we can directly access its callback
easyrtc.events.on("easyrtcAuth", function(socket, easyrtcid, msg, socketCallback, callback) {
easyrtc.events.defaultListeners.easyrtcAuth(socket, easyrtcid, msg, socketCallback, function(err, connectionObj){
if (err || !msg.msgData || !msg.msgData.credential || !connectionObj) {
callback(err, connectionObj);
return;
}
connectionObj.setField("credential", msg.msgData.credential, {"isShared":false});
console.log("["+easyrtcid+"] Credential saved!", connectionObj.getFieldValueSync("credential"));
callback(err, connectionObj);
});
});
// To test, lets print the credential to the console for every room join!
easyrtc.events.on("roomJoin", function(connectionObj, roomName, roomParameter, callback) {
console.log("["+connectionObj.getEasyrtcid()+"] Credential retrieved!", connectionObj.getFieldValueSync("credential"));
easyrtc.events.defaultListeners.roomJoin(connectionObj, roomName, roomParameter, callback);
});
// Start EasyRTC server
var rtc = easyrtc.listen(app, socketServer, null, function(err, rtcRef) {
console.log("Initiated");
rtcRef.events.on("roomCreate", function(appObj, creatorConnectionObj, roomName, roomOptions, callback) {
console.log("roomCreate fired! Trying to create: " + roomName);
appObj.events.defaultListeners.roomCreate(appObj, creatorConnectionObj, roomName, roomOptions, callback);
});
});
// listen on port
webServer.listen(port, function () {
console.log('listening on http://localhost:' + port);
});