我在express.js中有一个应用程序。我无法在HTTPS Web服务器上创建wss。
var fs = require('fs');
var express = require('express');
var app = express();
var cfg = {
ssl: true,
port: 8001,
ssl_key: 'sslcert/ssl.key',
ssl_cert: 'sslcert/ssl.crt'
};
var httpServ = (cfg.ssl) ? require('https') : require('http');
if (cfg.ssl) {
httpsServer = httpServ.createServer({
key: fs.readFileSync(cfg.ssl_key),
cert: fs.readFileSync(cfg.ssl_cert)
}, app)
.listen(cfg.port, function () {
console.log('Magic happening at https://Secure');
});
} else {
httpsServer = httpServ.createServer(app)
.listen(cfg.port, function () {
console.log('Magic happening at http://InSecure');
});
}
var WebSocketServer = require('websocket')
.server;
var wsServer = new WebSocketServer({
httpServer: httpsServer
});
wsServer.on('connection', function connection(ws) {
console.log('Socket Connected');
});
我的印象是将Web服务器的引用传递给WebSocket,然后WebSocket就会知道服务器的端口和SSL功能。但每次我在控制台中出现错误
与'ws:// localhost:8001 /?trackme = TUKOCVPAF'的WebSocket连接失败:在收到握手响应之前连接已关闭
如何在创建Websocket时创建wss:// localhost:8001 /?trackme = TUKOCVPAF'??