我想学习安全的webssockets,所以我试着按照教程进行操作 https://blog.zackad.com/en/2017/08/19/create-websocket-with-nodejs.html
我使用certbot生成了SSL证书(让我们加密),并将私钥和证书文件复制到我的项目中。
不幸的是,以下程序失败了,显然是在阅读时显示的错误。 什么可能出错?
// Minimal amount of secure websocket server
var fs = require('fs');
// read ssl certificate
var privateKey = fs.readFileSync('0000_key-certbot.pem', 'utf8');
var certificate = fs.readFileSync('0000_csr-certbot.pem', 'utf8');
var credentials = { key: privateKey, cert: certificate };
var https = require('https');
//pass in your credentials to create an https server
var httpsServer = https.createServer(credentials);
httpsServer.listen(8443);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: httpsServer
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('reply from server : ' + message)
});
ws.send('something');
});
错误:
_tls_common.js:88
c.context.setCert(options.cert);
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Object.createSecureContext (_tls_common.js:88:17)
at Server (_tls_wrap.js:803:25)
at new Server (https.js:54:14)
at Object.createServer (https.js:76:10)
at Object.<anonymous> (/Users/me/Desktop/TestHTTPS/test.js:12:25)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
我的认证文件看起来像这样(关键非常相似):
> -----BEGIN CERTIFICATE REQUEST-----
MIICezCCAWMCAQIwADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAM69
c39TcFkyLrQ1fCsCNFXCaG1ZXzp6JLWw/Sg/KHtHH8vHP2o/nWPjFr0OCsgpAQGS
NVDy28AcZzwkRVbytEguUUKjIFkpzUkbkxqHTzN8S+awQDGtIdaFrKirZjRIJIhK
t+CkMq6w25iD+SBU8TV2GVe+TG9q50Skgt+tDboUwn8oWJs9lMoauE3IaRTzCKAQ
mBJPgoC5PQLKxrtPp0SRwh2mSXByEQNbYIS0lpAmXrgkWGrn3OPsCD8+uBGzJL9E
JhucwjdtEGdyj11Pgbm0MYjmfJjUmGH3aqkcINdxdbWhPQtKzIT76cOq+KOT9vs7
6qts9RHm6R17cLJgk3ECAwEAAaA2MDQGCSqGSIb3DQEJDjEnMCUwIwYDVR0RBBww
GoIHbXlkcy5tZYIPdndhbHRlci5teWRzLm1lMA0GCSqGSIb3DQEBCwUAA4IBAQBc
EHsYLA5rP/zq7Eerj2DWyLieSdzKa3gXn69E1Y9T6nGRFOFAUigxuzWHUZ8OMEQh
Yqb21ms7hRbbxgpTtV7tALLlzhVa+YOcowj/uPMlVLNYAG51eJTTneEWzk0g/XM8
bZf10ueF3Om7Pdn94Dik+ShjCI7WEBoHaC4tBSYzwitYeaPrc1NYHpF5XjbXXp5O
K85KvCIgLqvlXXV59ax4JAgHSzlnR0y2ZxY7Vn4wEwU2RiSzt+aJnZoIPXUc4/aW
7YMVT3UMe9smT2QIIN2enFRf6XvaMmzUOPL2B5nvibSyByfoc5bxGc6DySZZ7v2T
stShlDo9oalYMxbW5ESf
-----END CERTIFICATE REQUEST-----