使用node.js和binary.js保护Websocket [英] secure websocket using node.js and binary.js

查看:79
本文介绍了使用node.js和binary.js保护Websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Debian Jessie,Apache,Node.js和Binary.js播放mp3文件,它可以在 http:// ws://上运行,但我无法使用 https:// wss://使其正常工作...服务器已启动,但在浏览器中出现此错误:

I am streaming an mp3 file using Debian Jessie, Apache, Node.js and Binary.js, it works using http:// and ws://, but I cannot make it working using https:// and wss://... The server starts but I get this error in the browser:

WebSocket连接到'wss://192.168.0.14:9000/'失败:错误建立连接:net :: ERR_CONNECTION_CLOSED

WebSocket connection to 'wss://192.168.0.14:9000/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

这是我正在尝试的:

index.js :

var https = require('https');
var fs = require('fs');

var hskey = fs.readFileSync('/etc/ssl/private/ssl-cert-snakeoil.key');
var hscert = fs.readFileSync('/etc/ssl/certs/ssl-cert-snakeoil.pem')

var options = {
    key: hskey,
    cert: hscert
};

httpsServer = https.createServer(options, function (req, res) {
    res.writeHead(200);
    res.end("Hi from HTTPS");
}).listen(8080);


var BinaryServer = require('/home/john/node_modules/binaryjs').BinaryServer;

var server = BinaryServer({ 
    port: 9000,
    server: httpsServer
});

// callback function
server.on('connection', function(client) {
    var file = fs.createReadStream('/var/www/html/mp3/audio.mp3');
    client.send(file);
});

index.html :

<!doctype html>
<html>
    <head>
        <title>Stream audio</title>
        <meta charset="utf-8">
        <script src="https://cdn.jsdelivr.net/binaryjs/0.2.1/binary.min.js"></script>
        <script>
        var client = new BinaryClient('wss://192.168.0.14:9000');

        client.on('stream', function(stream, meta){

            var parts = [];

            stream.on('data', function(data){
                parts.push(data);
            });

            stream.on('end', function(){
                var music = document.createElement('audio');
                music.src = (window.URL || window.webkitURL).createObjectURL(new Blob(parts));
                music.controls = 'controls';
                music.preload = '';
                document.body.appendChild(music);
            });

        });
        </script>
    </head>


    <body>



    </body>
</html>

推荐答案

使用Nginx代理简单地终止SSL,并将所有请求传递给NodeJS后端.

Simply terminate the SSL with Nginx proxy, and pass all request to NodeJS backend.

这是Nginx配置的代码段:

This is snippet for Nginx config:

     location /ws {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_redirect off;
      proxy_pass http://127.0.0.1:9000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_buffers 8 32k;
      proxy_buffer_size 64k;
      break;
    }

这篇关于使用node.js和binary.js保护Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆