// Load required modules var http = require("http"); // http server core module var…
- User
// Load required modules var http = require("http"); // http server core module var https = require('https'); 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('./lib/easyrtc_server'); // EasyRTC external module
// Set process name process.title = "node-easyrtc";
// Setup and configure Express http server. Expect a subfolder called "static" to be the web root. var app = express(); // app.use(serveStatic('static', {'index': ['index.html']})); app.use(serveStatic('public', {'index': ['index.html']}));
var port = process.env.PORT || 8080; // Start Express http server on port 8080 var webServer = http.createServer(app).listen(port);
// Start Socket.io so it attaches itself to Express server var socketServer = socketIo.listen(webServer, {"log level":1});
easyrtc.setOption("logLevel", "debug");
// 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 8080 webServer.listen(8080, function () { console.log('listening on http://localhost:'+port); }); info - EasyRTC: Starting EasyRTC Server (v1.0.0) on Node (v21.6.2) debug - EasyRTC: Emitting event 'startup' debug - EasyRTC: Running func 'onStartup' node:net:1980 throw new ERR_SERVER_ALREADY_LISTEN(); ^
Error [ERR_SERVER_ALREADY_LISTEN]: Listen method has been called more than once without closing. at Server.listen (node:net:1980:11) at Object. (/home/philip/nodejs-webrtc-video-stream-master/server.js:60:11) at Module._compile (node:internal/modules/cjs/loader:1378:14) at Module._extensions..js (node:internal/modules/cjs/loader:1437:10) at Module.load (node:internal/modules/cjs/loader:1212:32) at Module._load (node:internal/modules/cjs/loader:1028:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) at node:internal/main/run_main_module:28:49 { code: 'ERR_SERVER_ALREADY_LISTEN' }
Node.js v21.6.2
- Assistant
The error message you are seeing indicates that the
listenmethod has been called more than once on the same server instance without closing it first. This usually happens when you attempt to bind the server to a port that is already in use by another instance or if thelistenfunction is inadvertently called twice on the same server instance.Here is the relevant part of your code where this issue could arise:
var webServer = http.createServer(app).listen(port); // ... // Listen on port 8080 webServer.listen(8080, function () { console.log('listening on http://localhost:'+port); });In this part of the code,
webServer.listen(port)is called twice. Once immediately after the HTTP server is created, and again later with the separatewebServer.listen(8080, ...)call. You only need to calllistenonce.Here's how you can fix it:
var webServer = http.createServer(app); // Start the server listening once webServer.listen(port, function () { console.log('listening on http://localhost:'+port); });This should resolve the
ERR_SERVER_ALREADY_LISTENerror. By making this adjustment, your server should only calllistenonce during the startup process. - User
ncaught TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed. at Easyrtc.createObjectURL (easyrtc.js:4240:31) at Easyrtc.setVideoObjectSrc (easyrtc.js:4706:36) at onUserMediaSuccess (easyrtc.js:4960:22)
- Assistant
The error message you are seeing indicates that the
createObjectURLmethod on theURLAPI is failing, likely because it's not being used correctly. The methodURL.createObjectURLwas previously used to create a blob URL for media streams, but it has been deprecated in favor of directly using the media stream with HTML elements like<video>.To fix this, you should directly assign the media stream to the
srcObjectof the video element instead. Here's how you can do that:Step-by-Step Solution
-
Identify the Code Using
createObjectURL:Locate the part of your code (or in the EasyRTC library if you're modifying the source) that looks like this:
var videoElement = document.getElementById("videoElementId"); // Example line to find the video element videoElement.src = URL.createObjectURL(mediaStream); -
Replace
createObjectURLwithsrcObject:Modify the above code to use
srcObjectdirectly:var videoElement = document.getElementById("videoElementId"); videoElement.srcObject = mediaStream; -
Locate and Fix in
easyrtc.js:If the change needs to be made in the
easyrtc.jslibrary file, go to the line numbers mentioned in your error message and manually make this adjustment there. -
Re-compile and Test:
After making these changes, test your application to ensure that it is correctly handling media streams.
Why the Change?
The
URL.createObjectURLmethod was widely used in earlier implementations to create object URLs for blob objects. However, for media streams, the modern and recommended approach is to assign the stream directly to an element'ssrcObject. This is more straightforward and less error-prone:videoElement.srcObject = mediaStream;is the new standard and should work across all modern browsers.
This should resolve the issue you're facing with
createObjectURL. If you're working with a library like EasyRTC and modifying it directly, keep track of the changes to make updates easier with library updates. -
- User
this.createObjectURL = function(mediaStream) { var errMessage; if (window.URL && window.URL.createObjectURL) { return window.URL.createObjectURL(mediaStream); } else if (window.webkitURL && window.webkitURL.createObjectURL) { return window.webkit.createObjectURL(mediaStream); } else { errMessage = "Your browsers does not support URL.createObjectURL."; logDebug("saw exception " + errMessage); throw errMessage; } };
- Assistant
The function
createObjectURLin your code is using deprecated methods (URL.createObjectURLandwebkitURL.createObjectURL) to create a URL for amediaStream. These methods are no longer supported in modern browsers and should be replaced with thesrcObjectassignment method.Here's how you can update your code:
-
Remove or Disable
createObjectURLFunction:The
createObjectURLfunction is no longer needed for handling media streams in modern browsers. You can either remove it or modify it as a no-op (no operation) function. -
Use
srcObjectfor Assigning Media Streams:Instead of using
createObjectURL, directly assign the media stream to thesrcObjectof the video element. This would typically be used where you are about to display the media stream.
Example of Assigning a Media Stream to a Video Element:
Here's an example of how you can set a media stream to a video element:
function setVideoSource(videoElement, mediaStream) { if ("srcObject" in videoElement) { videoElement.srcObject = mediaStream; } else { // Fallback for older browsers that might not support srcObject videoElement.src = window.URL.createObjectURL(mediaStream); } videoElement.play(); } // Usage var videoElement = document.getElementById("videoElementId"); setVideoSource(videoElement, yourMediaStream);Steps to Integrate Changes:
- Locate all instances where
createObjectURLis being called and replace the calls with direct assignments usingsrcObject. - If your application needs to support very old browsers, keep a fallback that uses
URL.createObjectURL, but be aware that this does not cover the latest browsers or scenarios.
Conclusion
Transitioning to
srcObjectis essential for modern web development practices regarding media streams, and this update will ensure compatibility with current and future browser versions. -