Created
March 14, 2018 15:32
-
-
Save haydenjameslee/097239d7c5a620caf59901251ea5ffa2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
// 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(serveStatic('server/static', {'index': ['index.html']})); | |
// 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); | |
}); | |
// Node Get ICE STUN and TURN list | |
var https = require("https"); | |
var options = { | |
host: "xxx", | |
path: "xxx", | |
method: "PUT", | |
headers: { | |
"Authorization": "Basic " + new Buffer("xxx").toString("base64") | |
} | |
}; | |
easyrtc.on("getIceConfig", function(connectionObj, callback) { | |
var httpreq = https.request(options, function(httpres) { | |
var str = ""; | |
httpres.on("data", function(data){ str += data; }); | |
httpres.on("error", function(e){ console.log("error: ",e); }); | |
httpres.on("end", function(){ | |
var d = JSON.parse(str); | |
if(d.s == 'ok'){ | |
var iceConfig = d.v.iceServers; | |
console.log('server list: ',iceConfig); | |
callback(null, iceConfig); | |
} | |
}); | |
}); | |
httpreq.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment