Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Load required modules
  2. var http    = require("http");              // http server core module
  3. var express = require("express");           // web framework external module
  4. var serveStatic = require('serve-static');  // serve static files
  5. var socketIo = require("socket.io");        // web socket external module
  6. var easyrtc = require("easyrtc");               // EasyRTC external module
  7.  
  8. // Set process name
  9. process.title = "node-easyrtc";
  10.  
  11. // Get port or default to 8080
  12. var port = process.env.PORT || 8080;
  13.  
  14. // Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
  15. var app = express();
  16. app.use(serveStatic('public', {'index': ['index.html']}));
  17.  
  18. // Start Express http server
  19. var webServer = http.createServer(app).listen(port);
  20.  
  21. // Start Socket.io so it attaches itself to Express server
  22. var socketServer = socketIo.listen(webServer, {"log level":1});
  23.  
  24. var myIceServers = [
  25.   {"url":"stun:stun.l.google.com:19302"},
  26.   {"url":"stun:stun1.l.google.com:19302"},
  27.   {"url":"stun:stun2.l.google.com:19302"},
  28.   {"url":"stun:stun3.l.google.com:19302"}
  29.   // {
  30.   //   "url":"turn:[ADDRESS]:[PORT]",
  31.   //   "username":"[USERNAME]",
  32.   //   "credential":"[CREDENTIAL]"
  33.   // },
  34.   // {
  35.   //   "url":"turn:[ADDRESS]:[PORT][?transport=tcp]",
  36.   //   "username":"[USERNAME]",
  37.   //   "credential":"[CREDENTIAL]"
  38.   // }
  39. ];
  40. easyrtc.setOption("appIceServers", myIceServers);
  41. easyrtc.setOption("logLevel", "debug");
  42. easyrtc.setOption("demosEnable", false);
  43.  
  44. // Overriding the default easyrtcAuth listener, only so we can directly access its callback
  45. easyrtc.events.on("easyrtcAuth", function(socket, easyrtcid, msg, socketCallback, callback) {
  46.     easyrtc.events.defaultListeners.easyrtcAuth(socket, easyrtcid, msg, socketCallback, function(err, connectionObj){
  47.         if (err || !msg.msgData || !msg.msgData.credential || !connectionObj) {
  48.             callback(err, connectionObj);
  49.             return;
  50.         }
  51.  
  52.         connectionObj.setField("credential", msg.msgData.credential, {"isShared":false});
  53.  
  54.         console.log("["+easyrtcid+"] Credential saved!", connectionObj.getFieldValueSync("credential"));
  55.  
  56.         callback(err, connectionObj);
  57.     });
  58. });
  59.  
  60. // To test, lets print the credential to the console for every room join!
  61. easyrtc.events.on("roomJoin", function(connectionObj, roomName, roomParameter, callback) {
  62.     console.log("["+connectionObj.getEasyrtcid()+"] Credential retrieved!", connectionObj.getFieldValueSync("credential"));
  63.     easyrtc.events.defaultListeners.roomJoin(connectionObj, roomName, roomParameter, callback);
  64. });
  65.  
  66. // Start EasyRTC server
  67. var rtc = easyrtc.listen(app, socketServer, null, function(err, rtcRef) {
  68.     console.log("Initiated");
  69.  
  70.     rtcRef.events.on("roomCreate", function(appObj, creatorConnectionObj, roomName, roomOptions, callback) {
  71.         console.log("roomCreate fired! Trying to create: " + roomName);
  72.  
  73.         appObj.events.defaultListeners.roomCreate(appObj, creatorConnectionObj, roomName, roomOptions, callback);
  74.     });
  75. });
  76.  
  77. //listen on port
  78. webServer.listen(port, function () {
  79.   console.log('listening on http://localhost:' + port);
  80. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement