I'm testing all the steps on my server and everything worked ok until the step-05. then after watching some videos about webRTC it was clear that the problem was with https, webrtc no longer work on unsecured http sites, so I fixed the problem using the same ssl certificates that my apache configuration uses:
in the file index.js you have to change the following lines:
var http = require('https');
and also add this line:
const fs = require('fs');
var options = {
key: fs.readFileSync('/etc/apache2/ssl/yoursite.key'),
cert: fs.readFileSync('/etc/apache2/ssl/yoursite.crt')
};
and change the var app line with this:
var app = http.createServer(options,function(req, res) {
fileServer.serve(req, res);
}).listen(8080);
and problem solved!!!
this is my entire step-05 index.js remember to change your ssl certificates.
`'use strict';
var os = require('os');
var nodeStatic = require('node-static');
var http = require('https');
var socketIO = require('socket.io');
var fileServer = new(nodeStatic.Server)();
//Inicio parte ssl
const fs = require('fs');
var options = {
key: fs.readFileSync('/etc/apache2/ssl/yoursite.key'),
cert: fs.readFileSync('/etc/apache2/ssl/yoursite.crt')
};
var app = http.createServer(options,function(req, res) {
fileServer.serve(req, res);
}).listen(8080);
//fin parte ssl
var io = socketIO.listen(app);
io.sockets.on('connection', function(socket) {
// convenience function to log server messages on the client
function log() {
var array = ['Message from server:'];
array.push.apply(array, arguments);
socket.emit('log', array);
}
socket.on('message', function(message) {
log('Client said: ', message);
// for a real app, would be room-only (not broadcast)
socket.broadcast.emit('message', message);
});
socket.on('create or join', function(room) {
log('Received request to create or join room ' + room);
var numClients = io.sockets.sockets.length;
log('Room ' + room + ' now has ' + numClients + ' client(s)');
if (numClients === 1) {
socket.join(room);
log('Client ID ' + socket.id + ' created room ' + room);
socket.emit('created', room, socket.id);
} else if (numClients === 2) {
log('Client ID ' + socket.id + ' joined room ' + room);
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room, socket.id);
io.sockets.in(room).emit('ready');
} else { // max two clients
socket.emit('full', room);
}
});
socket.on('ipaddr', function() {
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
socket.emit('ipaddr', details.address);
}
});
}
});
socket.on('bye', function(){
console.log('received bye');
});
});
`