使用 sockjs 设置 SSL



如何在sockjs(在nodejs上运行)中安装https/ssl证书(和密钥)?

我目前正在运行最基本的设置,即"echo"示例设置。它是以下一个:

var http = require('http');
var sockjs = require('sockjs');
var node_static = require('node-static');
// 1. Echo sockjs server
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};
var sockjs_echo = sockjs.createServer(sockjs_opts);
sockjs_echo.on('connection', function(conn) {
    conn.on('data', function(message) {
        conn.write(message);
    });
});
// 2. Static files server
var static_directory = new node_static.Server(__dirname);
// 3. Usual http stuff
var server = http.createServer();
server.addListener('request', function(req, res) {
    static_directory.serve(req, res);
});
server.addListener('upgrade', function(req,res){
    res.end();
});
sockjs_echo.installHandlers(server, {prefix:'/echo'});
console.log(' [*] Listening on 0.0.0.0:9999' );
server.listen(9999, '0.0.0.0');

谢谢!

好的,

我通过将代码更改为:

var http = require('https');
var sockjs = require('sockjs');
var fs = require("fs");
var options = {
  key: fs.readFileSync('ssl.key'),
  cert: fs.readFileSync('ssl.crt')
};
var echo = sockjs.createServer();
echo.on('connection', function(conn) {
    conn.on('data', function(message) {
        conn.write(message);
    });
    conn.on('close', function() {});
});
var server = http.createServer(options);
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '0.0.0.0');

相关内容

  • 没有找到相关文章

最新更新