Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

navigator.mediaDevices undefined when serving ip address #2

Open
kmturley opened this issue Nov 2, 2019 · 0 comments
Open

navigator.mediaDevices undefined when serving ip address #2

kmturley opened this issue Nov 2, 2019 · 0 comments

Comments

@kmturley
Copy link

kmturley commented Nov 2, 2019

Thanks for the great demo! I came across this issue when testing across a local network using ip addresses.

Browsers only allow you to access navigator.mediaDevices on http://localhost OR secure https connections.

You can override this option on Chrome using the startup flag:
--unsafely-treat-insecure-origin-as-secure="http://example.com"

But otherwise its really hard to develop and test this. So a better way is to add https support to the project.

Install the node https server:
npm install https

update server.js to allow switching between secure:

const ADDRESS = '0.0.0.0';
const PORT = 8080;
const MAX_CLIENTS = 50;
const secure = true;

let os = require('os');
let app = null;
let host = '';
if (secure === true) {
    const fs = require('fs');
    const options = {
        key: fs.readFileSync('./client-key.pem'),
        cert: fs.readFileSync('./client-cert.pem')
    };
    host = 'https';
    app = require('https').createServer(options, handler);
} else {
    host = 'http';
    app = require('http').createServer(handler);
}

let io = require('socket.io')(app);

app.listen(PORT, ADDRESS);
console.log(`Socket.io server listening on: ${host}://${ADDRESS}:${PORT}`);

update lib.js with

this.socket = io.connect(`//${this.ip}:${this.port}`);

Generate a local key and certificate:

openssl genrsa -out client-key.pem 2048
openssl req -new -key client-key.pem -out client.csr
openssl x509 -req -in client.csr -signkey client-key.pem -out client-cert.pem

Then start the local server using:
http-server --ssl --cert ./client-cert.pem --key ./client-key.pem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant