【问题标题】:socket.io.js not found on client在客户端上找不到 socket.io.js
【发布时间】:2013-10-22 02:54:35
【问题描述】:

之前有人问过这个问题,但在之前的回复中没有找到任何解决方案。

Socket.IO 给了我两个问题:

  1. 服务器端出现此错误 - 错误 - 监听 EACESS 我阅读了 stackoverflow 并通过发出 sudo 命令来启动服务器解决了这个问题。
  2. 现在客户端似乎没有根据脚本行找到 socket.io.js 文件 -

我了解使用 chrome 开发人员工具控制台找不到文件,文件上有 404 错误。

我读到这个文件是由服务器动态创建的。但我在根文件夹上做了一个“ls-a”。找不到 socket.io/socket.io.js 文件。

有什么想法吗?

这里是我的服务器代码供参考-

var http = require('http'),
  path = require("path"),
  url = require("url"),
  fs = require("fs"),
  mime = require("mime"),
  io = require("socket.io").listen(server);
var homepath = ".";
var server = http.createServer(function (req, res) {
  var uri = url.parse(req.url).pathname;
  var filepath = path.join(homepath, uri);
  console.log(filepath);
  path.exists(filepath, function (exists) {
    if (!exists) {
      //404 response
      res.writeHead(404, {
        "Content-Type": "text/plain"
      });
      res.write("404 File not Found \n");
      res.end();
    } else {
      if (fs.statSync(filepath).isDirectory()) {
        filepath += '/index.html';
        filepath = path.normalize(filepath);
      }
      fs.readFile(filepath, "binary", function (err, data) {
        if (err) {
          res.writeHead(500, {
            'Content-Type': 'text/plain'
          });
          res.write('500 File read error \n');
          res.end();
        } else {
          var contentType = mime.lookup(filepath);
          res.writeHead(200, {
            'Content-Type': contentType
          });
          res.write(data, 'binary');
          res.end();
        }
      });
    }
  });
  //sockets part starts here
  io.sockets.on('connection', function (socket) {
    socket.on('test', function (data) {
      console.log('i got something');
      console.log(data.print);
    });
  });
});
server.listen(3000);
server.on('error', function (e) {
  console.log(e);
});
console.log('Server listening on Port 3000');

【问题讨论】:

  • 你是用 npm 安装的吗?
  • 是的,我确实是通过 npm 安装的。

标签: javascript node.js socket.io


【解决方案1】:

您需要的客户端文件位于此处的 node_modules 文件夹中:

node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js

Socket.io 应该提供这个文件,所以你不需要将它复制到其他地方。例如,如果您在以下位置运行 socket.io 服务器:

http://localhost:5000

那么脚本将从:

http://localhost:5000/socket.io/socket.io.js

如果您从另一个应用程序或另一个端口使用 socket.io,则代码示例中的相对 URL 将不起作用。您需要手动包含客户端脚本或尝试包含客户端节点模块(如果由节点应用程序使用)。

您可以在此处查看客户端存储库以获取更多信息: https://github.com/LearnBoost/socket.io-client

【讨论】:

  • 我只使用 node.js。端口是 3000。但是我在 nodejs 中手动创建了一个文件服务器,而不使用 express。因此,服务器代码会尝试查找位于 socket.io/socket.io.js 的文件,但它不会找到。那么事情会如何发展呢?
  • socket.io/socket.io.js 的磁盘上没有文件。它由 socket.io 从不同的位置动态提供(请参阅此处的方法github.com/LearnBoost/socket.io/blob/master/lib/index.js#L145)。如果您自己处理它,则可能永远不会针对此请求访问 socket.io。
【解决方案2】:

这里的问题是您告诉 Socket.IO 侦听一个尚不存在的服务器,导致 EACCES 并因此不提供客户端文件。这就是你正在做的:

// the HTTP server doesn't exist yet
var io = require('socket.io').listen(server);
var server = http.createServer();

如果您在服务器端错误控制台中看到,您会得到:

info: socket.io started
warn: error raised: Error: listen EACCES

要解决此问题,请将您的监听功能移至服务器创建后:

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

一旦 Socket.IO 正确监听,它会自动将客户端文件提供给/socket.io/socket.io.js。您无需手动查找或提供服务。

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2018-01-26
    相关资源
    最近更新 更多