node.js - 需要有关 Socket.io.js 加载失败的帮助

标签 node.js apache ssl socket.io

我刚刚从 GoDaddy 获得了我的 ssl 证书,我现在正在运行 https://example.com 中的网站。和我的 socketio 服务器在 http://example.com:3000 (无法访问https中的socketio)

这是我从控制台收到的错误消息

“加载源“https://example.com:3000/socket.io/socket.io.js ”失败

这就是我添加 socket.io.js 的方式

<script src="<?php print BASE_URL; ?>:3000/socket.io/socket.io.js"></script>

这是我的带有 socket.io 的 nodejs 服务器

var app = require('express')();
var cors = require('cors');
var https = require('https').Server(app);
var io = require('socket.io')(https);
var _ = require('lodash');

// more include files here ...

https.listen(3000, function() {
  utils.logger('Listening on *: 3000', 'info');
});

io.use(function(socket, next) {
  let found = _.findIndex(socketStore.connections, { 'userId': socket.request._query.userId });

if (parseInt(socket.request._query.userId) !== 0) {
  if (found < 0) {
    socketStore.connections.push({
      userId: parseInt(socket.request._query.userId),
      socket: socket
    });
  }
  else {
    socketStore.connections[found] = {
      userId: parseInt(socket.request._query.userId),
      socket: socket
    };
  }

  utils.logger(`User connected from ${socket.request.connection.remoteAddress}`, 'info');
  utils.logger(`User ${socket.request._query.userId} is now online!`, 'info');

    next();
  }
});

// Socket IO
io.on('connection', function(socket) {
    // some stuffs
});

app.use(cors({ origin: '*' }));

// Routes and Endpoints
app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

这是我默认的 apache ssl 配置

<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    ServerAdmin webmaster@example.com
    ServerName example.com

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on

    SSLCertificateFile /root/ssl/example.com.crt
    SSLCertificateKeyFile /root/ssl/example.com.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
      SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
      SSLOptions +StdEnvVars
    </Directory>
    <Directory "/var/www/html">
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>

  </VirtualHost>
</IfModule>

最佳答案

您的错误信息:

“加载源“https://example.com:3000/socket.io/socket.io.js ”失败

正在尝试加载 /socket.io/socket.io.js来自 HTTPS服务器,但您显示的代码是针对使用 HTTP 的 socket.io 服务器的, 不是 HTTPS .您没有向我们展示它来自的网页,而是向我们展示了 <script>用于加载 socket.io.js 库的标记需要与您的 socket.io 服务器正在监听的协议(protocol)相同。

如果您已经有一个 https 服务器,那么您也应该将其用于 socket.io。没有必要只为 socket.io 创建一个单独的 http 服务器,事实上,访问 http 是一种不好的做法。来自 https 的资源页面,因为它在页面的安全性方面撒谎/欺骗用户。

关于node.js - 需要有关 Socket.io.js 加载失败的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571295/

相关文章:

r - 在 Ubuntu 16.04 Apache/2.4.18 (Ubuntu) 中安装 RApache : I get some errors

python - 在 Python2.7.9 的形式源安装后我得到 ssl.SSLError : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl. c:581)

windows - 在 Windows 上安装 Node 包时出现 ETIMEDOUT 错误

node.js - Chrome "Take Heap Snapshot"永远不会完成

python - Flask看不到环境变量

apache - 除了Apache中的几页之外,如何强制重写为HTTPS?

ssl - 在 Kafka 上启用 SSL

http - 启用 SSL 的站点和链接

javascript - Express.js响应自动将snake_case键转换为camelCase

html - Electron 应用程序中 html 内容的模块化