【问题标题】:How to modify my script socket.io to https?如何将我的脚本 socket.io 修改为 https?
【发布时间】:2018-02-03 18:34:10
【问题描述】:

我是 socket.io 的新手,我想在我的网站上使用我的应用程序,该应用程序目前在 HTTPS 上运行,所以实际上我有我的服务器:

var http = require('http').createServer(server),
fs = require('fs'),
io = require('socket.io')(http);

function server(req, res){
fs.readFile('index.html',(err, data)=>{
    if(err){
        res.writeHead(500,{'Content-Type':'text/html'});
        return res.end('<h1>Error</h1>');
    } else {
        res.writeHead(200,{'Content-Type':'text/html'});
        return res.end(data,'utf-8');
    }
})
}

http.listen(3000);
console.log("Running server");

io.on('connection', (socket)=>{
socket.emit("hello",{ message : 'Hello world'})

})

客户端索引.html:

<script src="/socket.io/socket.io.js"></script>
<script>        
        'use strict'
        var io =  io(),
        d = document

        io.on('hello', function(data){
            console.log(data);
            d.querySelector("#hello").innerHTML = data.message;
        })
</script>

这在 HTTP 中运行良好,但如何针对 HTTPS 进行修改?

编辑:

根据node docs,我更改了这样的服务器脚本:

var http = require('https'),
fs = require('fs'),
io = require('socket.io')(http);

var options = {
  key: fs.readFileSync('myprivkey.pem'),
  cert: fs.readFileSync('mycert.pem')
};

http.createServer(options, (req,res)=>{

fs.readFile('index.html',(err, data)=>{
    if(err){
        res.writeHead(500,{'Content-Type':'text/html'});
        return res.end('<h1>Error</h1>');
    } else {
        res.writeHead(200,{'Content-Type':'text/html'});
        return res.end(data,'utf-8');
    }
})
}).listen(3000);

console.log("Running server");

io.on('connection', (socket)=>{
   socket.emit("hello",{ message : 'Hello world'})
})

我运行服务器并且没有显示错误但客户端无法运行,如果我在浏览器中运行 mysite.com:3000/myapp,控制台会显示:'Uncaught SyntaxError: Unexpected token ' 和'Uncaught TypeError: io is not a function'.

如果我运行 mysite.com/myapp 控制台会说:'GET www.mysite.com/socket.io/socket.io.js 404(未找到)'和'未捕获的 TypeError:io 不是函数'。

我想在输入 mysite.com/myapp 时运行,“myapp”是服务器脚本和客户端 index.html 所在的文件夹名称

还有什么需要修改的?

【问题讨论】:

  • 您的域是否有公钥/私钥?你可以在 nodejs.org 上关注the docs。
  • 是的,我有我的密钥和证书,我的网站实际上是在 HTTPS 上运行的
  • 只需启动 https 服务器而不是 http 服务器,然后将该服务器与 socket.io 一起使用。使用https 模块,而不是http 模块。

标签: javascript sockets websocket socket.io


【解决方案1】:

按照您的要求,有两种通用方法。通常,在生产环境中,基于 nodejs 的服务器使用所谓的反向代理运行。这基本上是 nginx 或其他位于前面并传递节点需要关心的网络请求的 Web 服务器。在这种场景下,反向代理通常处理的是 HTTPS 端,node 只需要处理 http。

另一种方法是让节点处理 https 本身的 TLS 安全性。为此,您必须获得一个 https 证书(letsencrypt 在这里是一个不错的选择),并像这样告诉您的服务器实例:

var http = require('http').createServer(server),
fs = require('fs');

// Add https certificate
var crypto = require('crypto');
var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();
var credentials = crypto.createCredentials({key: privateKey, cert: certificate});

http.setSecure(credentials);

var io = require('socket.io')(http);

function server(req, res){
    fs.readFile('index.html',(err, data)=>{
        if(err){
            res.writeHead(500,{'Content-Type':'text/html'});
            return res.end('<h1>Error</h1>');
        } else {
            res.writeHead(200,{'Content-Type':'text/html'});
            return res.end(data,'utf-8');
        }
    })
}

http.listen(3000);
console.log("Running server");

io.on('connection', (socket)=>{
    socket.emit("hello",{ message : 'Hello world'})
})

【讨论】:

  • http.setSecure() 是什么?为什么不使用https.createServer()?
  • 感谢您的回答,第一种方法不是我的情况,因为这仅在我使用 http 输入 url 时才有效,而第二种方法不起作用,因为它说加密已被弃用并且当我运行服务器时显示:(节点)crypto.createCredentials 已弃用。请改用 tls.createSecureContext。 http.setSecure(凭证); TypeError: http.setSecure 不是函数
  • @robertklep,我想我找到的文档一定是旧的。我会尝试使用更新的代码进行更新。
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2023-03-18
  • 2017-12-23
  • 1970-01-01
  • 2023-03-23
  • 2021-08-15
  • 1970-01-01
相关资源
最近更新 更多