2

我购买了 GoDaddy 标准 SSL 证书并将其安装在http://mysite.com上。现在https://mysite.com运行良好。

现在,我想为在http://mysite.com:3000运行的 socket.io 安装证书

我尝试通过以下方式连接 SSL:

var socket = io.connect('https://www.mysite.com:3000', {secure:true}); 

但它不起作用。这是 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"});
}

和客户端 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>

有什么建议么?

4

1 回答 1

4

您的客户正在连接到http,请尝试让您的客户 index.html 连接到https

此外,您需要加载证书。

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);

创建您的自签名.pem文件。(要获得奖励积分,请.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
于 2013-03-09T17:05:00.257 回答