gpt4 book ai didi

javascript - NodeJs 将 'net' 套接字转换为基于 SSL/TLS 的套接字?

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:21 24 4
gpt4 key购买 nike

我在服务器(聊天引擎)上运行了以下 nodejs 代码。我想将其转换为基于安全 SSL/TLS 的连接。我该怎么做?

在客户端(见下面的代码),每次我试图将其转换为 SSL 时,它都会给我一个“跨源请求失败”的错误。我不知道为什么?

NODE.JS 服务器端代码

var cluster = require('cluster');
var net = require('net');
var fs = require('fs');
var config = require('./config.js');
var num_processes = 1;//require('os').cpus().length;

if (cluster.isMaster) {

// This stores our workers. We need to keep them to be able to reference
// them based on source IP address. It's also useful for auto-restart,
// for example.
var workers = [];

// Helper function for spawning worker at index 'i'.
var spawn = function (i) {
workers[i] = cluster.fork();

// Optional: Restart worker on exit
workers[i].on('exit', function (worker, code, signal) {
if (config.server.restart_instances_on_crash) {
spawn(i);
logging.log('debug', 'Node instances exited, respawning...');
}
});
};

// Spawn workers.
for (var i = 0; i < num_processes; i++) {
spawn(i);
}

// Helper function for getting a worker index based on IP address.

var worker_index = function (ip, len) {
var s = '';
for (var i = 0, _len = ip.length; i < _len; i++) {
if (ip[i] !== '.') {
s += ip[i];
}
}

return Number(s) % len;
};

/* wait 5 seconds to make sure all the instances are running and initialized */

setTimeout(function () {
// Create the outside facing server listening on our port.

var options = {
pauseOnConnect: true
};
var server = net.createServer(options, function (connection) {
// We received a connection and need to pass it to the appropriate
// worker. Get the worker for this connection's source IP and pass
// it the connection.
var str = connection.remoteAddress;
var ip = str.replace("::ffff:", '');
var worker = workers[worker_index(ip, num_processes)];
worker.send('sticky-session:connection', connection);
}).listen(config.server.listenport);
logging.log('debug', 'Server listening on ' + config.server.listenip + ':' + config.server.listenport + '...');

}, 5000);
process.on('uncaughtException', function (error) {
logging.log('error', 'uncaughtException');
logging.log('error',error.stack);
process.exit(1);
});

} else {
var express = require('express');
var sio = require('socket.io');
var sio_redis = require('socket.io-redis');

var dateandtime = require('./includes/dateandtime.js');
var stats = require('./includes/stats.js');
var helper = require('./includes/helper.js');

// Note we don't use a port here because the master listens on it for us.
var app = new express();

// Don't expose our internal server to the outside.
var server = app.listen(0, 'localhost'),
io = sio(server);
io.set('origins', '*:*');

}

在我的客户端,我有以下基于代码的 socket.io JS 模块。 (仅使用 net 模块时,所有这些在没有 SSL 连接的情况下都可以正常工作)

以下是使用 socket.io 函数连接到 Node 服务器的客户端 javascript 代码。

var root_url = 'http://'+window.location.hostname;
var socket_io = 'http://example.com/js/chat/socket.io-1.3.5.js';
$(document).ready(function () {
$.getScript(socket_io, function () {
socket = io.connect(root_url + ':8440');
... etc

最佳答案

只能提示您检查跨域请求的 cors 包,但更深层次的问题可能是并非所有内容都在 SSL 中运行。 (混合 http 和 https)

关于javascript - NodeJs 将 'net' 套接字转换为基于 SSL/TLS 的套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com