Socket.io [HTTP / 1.1 404 Not Found error

时间:2015-07-21 17:42:35

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

我正在使用带有Socket.io的Node.js,并在端口转发服务器或在我自己的计算机上运行时使一切工作正常。但是,我将服务器添加到我的Web主机,我现在收到错误

" [HTTP/1.1 404 Not Found" while it tries to get http://sitename/socket.io/

我真的不确定导致此错误的原因,并希望有人可能会看到我的代码出错。

客户:

<!doctype html>
<html>
  <head>
    <title>Test 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; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
    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));
      });
    </script>
  </body>
</html>

服务器:

var express = require('express')
  , cors = require('cors')
  , http = require('http').Server(app)
  , io = require('socket.io')(http)
  , app = express();

app.use(cors());


app.get('/', function(req, res, next) {
    //res.sendFile(__dirname + '/index.html');
    res.json({msg: 'This is CORS-enabled for all origins!'});
});

app.post('/', function(req, res, next) {
    // Handle the post for this route
});

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

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

0 个答案:

没有答案