简体   繁体   中英

Is it possible to have an SSL page from NGINX (port 443) also use socket.io (through Node.js) on port 3000

Im have developed a Node.js application that uses socket.io to emit, in real time, some JSON array based content out to anywhere from 10 to 100 client machines.

It works great and I'm blown away by the speed and reliability of Node.js, and I'm now preparing to move it to production and want to utilize https (which I have successfully configured on NGINX (running on Ubuntu 14.04 server on my network).

The problem I have run into is that when I use the HTTPS (ssl) version of the URL, the page loads fine,but as for the web socket, I get a socket error due to mixed content. (note: I have NGINX setup to server Both SSL and Non-SSL version for now, but eventually I want it to only serve SSL version)

I have tried (in the CLIENT) to use https for the socket connection like so:

var socket = io.connect('https://mycooldomaingoeshere.com:3000');

Here is the code (server.js and test.html) I'm using successfully with the NON SSL test:

//FILE NAME: SERVER.JS

var app = require('express')();
var http = require('http').Server(app); // for node to serve clients
var io = require('socket.io')(http); // socket.io bidirectional from node.js to clients

// THE JSON DATA I WILL EMIT TO CONNECTED CLIENTS...
var myJSON = [{"array":[1,2,3],"boolean":true,"null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Hello World"}];

// THE HTML PAGE THAT I WILL SERVE TO CLIENTS CONNECTED ON PORT 3000...
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/test.html');  // note, all connecting clients get served this ONE page
});

// UPON SUCCESSFUL CONNECTION TO THE PORT (3000)...
io.sockets.on('connection', function(socket) {

    console.log('connected the new client!');

    setInterval(function(){
        // EMIT OUT THE JSON ARRAY TO CLIENTS EVERY 3 SECONDS
        socket.volatile.emit('passingJSON', myJSON); //emit to all clients
    }, 3000);
}); 

// OK, LETS LISTEN FOR ANY CLIENTS TRYING TO CONNECT ON PORT 3000
http.listen(3000, function() { //listen to 3000
    console.log('listening on *:3000...');
});

Here is the page that clients are served ( test.html ):

<html>
<head>

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

<script>

$(document).ready(function(){
     var socket = io.connect('http://localhost:3000');

    // HERES SOMETHING THE NODE.JS SERVER CAN 'CALL' OR INVOKE...
    socket.on('passingJSON', function(data) {
            console.log(data);
            console.log(new Date().toLocaleString());  
    });
});

</script>
</head>

<body>
    this is a test page that uses node.js and socket.io
</body>
</html>

Node and NPM package versions I'm using are:

  • socket.io v1.4.5
  • express v4.13.4
  • npm v3.9.5
  • node.js v6.2.2

My question is what do I need to add to my code or do differently for socket.io to be able to work within / alongside the HTTPS origin of the parent page?

I will suggest just use client side connection like this:

var socket = io.connect();

what this should do is adapt to https and http automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM