I'm not too familiar with Javascript but I'm on a run now because I have been assigned an assignment for academic purposes to create a Live Streaming website application using WebRTC.
I have been following the Codelab tutorial, but since it uses an HTTP server it really won't work in Chrome (my guess, not really sure about it since it works on Firefox).
I would like to know how could I make this server a HTTPS server and how could I access to it using the nat network or my public router ip address (if it's possible) instead of the localhost. I've already generated my .key and my .crt.
These are the codes by Codelab:
index.js (server)
'use strict';
var os = require('os');
var nodeStatic = require('node-static');
var http = require('http');
var socketIO = require('socket.io');
var fileServer = new(nodeStatic.Server)();
var app = http.createServer(function(req, res) {
fileServer.serve(req, res);
}).listen(8080);
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');
});
});
packaje.json (in case the code is needed)
{
"name": "webrtc-codelab",
"version": "0.0.1",
"description": "WebRTC codelab",
"dependencies": {
"node-static": "0.7.7",
"socket.io": "1.2.0"
}
}
My attempt of HTTPS server
'use strict';
var os = require('os');
var express = require('express');
var nodeStatic = require('node-static');
var http = require('http');
var https = require('https');
var fs = require('fs');
var socketIO = require('socket.io');
var options = {
key: fs.readFileSync('host.key'),
cert: fs.readFileSync('host.cert')
};
var fileServer = new(nodeStatic.Server)();
var app = express();
//http.createServer(app).listen(80);
http.createServer(app,function(req,res){
fileServer.serve(req,res);
}).listen(80);
https.createServer(options,app,function(req,res){
fileServer.serve(req,res);
}).listen(443);
//var app = http.createServer(function(req, res) {
// fileServer.serve(req, res);
//}).listen(8080);
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');
});
});
The error is when I try: var io = socketIO.listen(app); and I don't really know why it works with only the http, and not with both the http and https server created in app.
I would really appreciate your help!
via maga
No comments:
Post a Comment