[英]Installing SSL On Socket.IO Node.js
I bought GoDaddy Standard SSL certificate and installed it on http://mysite.com . 我购买了GoDaddy Standard SSL证书,并将其安装在http://mysite.com上 。 Now https://mysite.com works well. 现在https://mysite.com运作良好。
Now, I want to install the certificate for socket.io which is running at http://mysite.com:3000 现在,我要安装在http://mysite.com:3000上运行的socket.io的证书
I tried to connect with SSL by doing: 我尝试通过以下方式连接SSL:
var socket = io.connect('https://www.mysite.com:3000', {secure:true});
but it's not working. 但它不起作用。 Here is the server.js 这是server.js
var io = require('socket.io'),
connect = require('connect'),
mysql = require('mysql');
var app = connect().use(connect.static('../public_html/node/'));
app.listen(3000);
var server = io.listen(app);
server.sockets.on('connection', function(socket) {
socket.emit('getid', {message: "A New user is online"});
}
and the client index.html 和客户端index.html
<script type = "text/javascript" src = "http://www.mysite.com:3000/socket.io/socket.io.js"></script>
<script type = "text/javascript">
var socket = io.connect('http://www.mysite.com:3000');
</script>
Any suggestions? 有什么建议么?
Your clients are connecting to http
, try having your client index.html connect to https
. 您的客户端正在连接到http
,请尝试将客户端index.html连接到https
。
Additionally, you need to load a certificate. 此外,您需要加载证书。
const crypto = require('crypto'),
fs = require("fs"),
http = require("http");
var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();
var credentials = crypto.createCredentials({key: privateKey, cert: certificate});
var handler = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
};
var server = http.createServer();
server.setSecure(credentials);
server.addListener("request", handler);
server.listen(8000);
Create your self-signed .pem
files. 创建您的自签名.pem
文件。 (For bonus points, get your .csr
signed by a reputable company.) (要获得奖励积分,请由知名公司签署您的.csr
。)
openssl genrsa -out privatekey.pem 2048
openssl req -new -key privatekey.pem -out certrequest.csr
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.