【问题标题】:socket.io dont work on server and have 404 errorsocket.io 不能在服务器上工作并且有 404 错误
【发布时间】:2017-12-11 10:06:10
【问题描述】:

我在我的服务器中创建了一个简单的 socket.io 项目,但我有错误 我正在处理的IP是http://5.61.25.90/~socket 所有文件都存储在 public_html 中,URL 为:http://5.61.25.90/~socket/public_html

&这是我的代码

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
      #messages { margin-bottom: 40px }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      $(function () {
        var socket = io();
        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
          $('#m').val('');
          return false;
        });
        socket.on('chat message', function(msg){
          $('#messages').append($('<li>').text(msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
      });
    </script>
  </body>
</html>

上面的代码是 index.html 文件,底部是 index.js

 var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var port = process.env.PORT || 3000;

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

    io.on('connection', function(socket){
      socket.on('chat message', function(msg){
        io.emit('chat message', msg);
      });
    });


    http.listen(port, function(){
      console.log('listening on *:' + port);
    });

现在控制台面板出现 404 错误

喜欢这个

http://5.61.25.90/socket.io/?EIO=3&transport=polling&t=LqRzAYF 404 未找到 socket.io.js(第 4948 行)

【问题讨论】:

    标签: javascript node.js sockets express socket.io


    【解决方案1】:

    在我看来,这可能会有所帮助:

    app.get('*', function(req, res) {
    

    而不是这个:

    app.get('/', function(req, res) {
    

    您在后端的应用路由器仅捕获路由'/'。所以它无法处理与路由不完全匹配的请求。

    【讨论】:

    • 您的节点服务器在哪个端口上运行?如果不是 80 端口,则需要在 URL 中指定它,例如 http://localhost:3000 端口 3000。
    • 即ERR_CONNECTION_timedout
    【解决方案2】:

    看起来您正在服务器上使用 Apache2 虚拟主机。如果我是正确的,请尝试将这两行添加到您的 Apache 配置文件中:

    ProxyPass /socket.io/socket.io.js http://127.0.0.1:3000/socket.io/socket.io.js
    ProxyPass /socket.io ws://127.0.0.1:3000/socket.io/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 2017-12-28
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2021-10-14
      相关资源
      最近更新 更多