【问题标题】:socket.io: 404 errors in javascript consolesocket.io:javascript 控制台中的 404 错误
【发布时间】:2020-03-24 15:28:43
【问题描述】:

在谷歌浏览器中,example.com/socket.io 给出 404 错误。 所以看起来我的 socket.io 配置有错误。 我不知道它们是什么。 以下是我的 node.js 服务器中最重要的部分:

Server.js

"use strict";
/** Imported modules */
const express = require("express");
const server = express();
var app = require("http").createServer(server);

const io = require("socket.io")(app);

const request = require("./modules/request.js");
const db = require("./modules/database.js")(bcrypt, sequelize);
const io_control = require("./modules/io_control.js")(io, db, sequelize);

/** Page Control Modules */
const PageConfig = require("./modules/page/PageConfig.js");
const CommonItems = require("./modules/page/CommonItems.js");

server.use(express.urlencoded({ extended: true }));
io.origins("*:*");
const Port = process.env.PORT || 3000;
app.listen(Port);

io.on("connection", function(socket) {
  console.log(true);
});

io_control.js

module.exports = (io, db, sequelize) => {
  var controller = new IOControl(io, db, sequelize);

  io.on("connection", function(socket) {
    console.log(true);
    //and a lot of other code inside here.
  });
};

在客户端:它是这样的:

第一个脚本

<script src="/socket.io" type="application/javascript"></script>

第二个脚本

const socket = io('//web.namei.nl',{path: '/socket.io'},{transports: ['websocket'], upgrade: false}).connect('', {query: `uuid=${getCookie('uuid')}&type=${getCookie('type')}` });

我尝试更改两个脚本,但没有结果。

【问题讨论】:

  • 强烈建议:获取 Postmancurl 以验证您是否可以使用所需的 URL 连接到所需的服务器。
  • 请分享您连接到socket.io服务器的客户端代码
  • @SamuelG 刚刚做了,编辑了问题:)
  • 所以这是服务器的位置://web.namei.nl?

标签: node.js express socket.io http-status-code-404


【解决方案1】:

由于 socket.io 客户端脚本源路径:src="/socket.io",您收到 404,这可能无法解析,因为路径似乎无效。

要么使用 CDN,指向 IO 服务器上的位置,例如://web.namei.nl/socket.io/socket.io.js,要么直接下载文件并将其包含在客户端中,或者使用捆绑器。

来自 OP 的其他 cmets 后更新

如果您无法连接到套接字服务器并且确定 URL 正确,那么:

  1. 当您使用 express 来托管您的 IO 时,您应该启用 CORS
  2. 尝试在不使用 websocket 作为传输的情况下进行连接,因为您的托管服务可能存在限制。
  3. 确保您的连接选项配置正确 - 它们似乎不正确。
  4. 如果您想限制哪些域可以访问服务器,请仅使用 io.origins

在服务器端:

const cors = require('cors');
...
const app = express();
app.use(cors());

const http = require('http').createServer(app);
const io = require('socket.io')(http);

io.on('connection', (socket) => {
   ...
});

const port = process.env.PORT || 3000;
http.listen(port, () => {
    console.log(`listening on ${port}`);
});

客户:

// ensure the URL including port are correct below, also first connect without the additional query option
const socket = io('https://web.namei.nl', {
   query: `uuid=${getCookie('uuid')}&type=${getCookie('type')}`
});

socket.on('connection', () => {
   console.info('connected');
});

【讨论】:

  • 我都试过了。当我这样做时,我在控制台中遇到了 404 错误,例如 https://web.namei.nl/socket.io/?EIO=3&amp;transport=polling&amp;t=Mx18Wp-。也许这甚至不是我的错,它依赖于虚拟主机提供商。
  • 尝试在您的连接选项中删除 websocket 传输
  • 我该怎么做?
  • 连接语句语法看起来不正确,我正在更新答案
猜你喜欢
  • 2012-10-06
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多