[英]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. 我开发了一个Node.js应用程序,该应用程序使用socket.io实时发出一些基于JSON数组的内容,这些内容可以发送到10到100台客户端计算机中的任何位置。
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). 它运行良好,我对Node.js的速度和可靠性感到震惊,现在我正准备将其移至生产环境,并希望使用https(我已在NGINX上成功配置了https(在Ubuntu 14.04服务器上运行)我的网络)。
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. 我遇到的问题是,当我使用URL的HTTPS(ssl)版本时,页面可以正常加载,但是对于Web套接字,由于内容混合,我收到了套接字错误。 (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) (注意:目前我已经将NGINX设置为同时服务于SSL和非SSL版本,但最终我希望它仅提供SSL版本)
I have tried (in the CLIENT) to use https for the socket connection like so: 我已经尝试(在客户端中)将https用于套接字连接,如下所示:
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: 这是我在NON SSL测试中成功使用的代码(server.js和test.html):
//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 ): 这是为客户提供服务的页面(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: 我正在使用的Node和NPM软件包版本是:
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? 我的问题是我需要添加什么代码或使socket.io能够在父页面的HTTPS起源内/旁边工作?
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. 这应该做的是自动适应https
和http
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.