简体   繁体   English

Socket.io版本2.1.0错误404

[英]Socket.io version 2.1.0 error 404

I found this error using socket.io latest version: "index.js: 83 POST http: //localhost/socket.io/? EIO = 3 & transport = polling & t = MDUHEO9 404 (Not Found)". 我使用socket.io最新版本发现了此错误:“ index.js:83 POST HTTP://localhost/socket.io/?EIO = 3&transport = polling&t = MDUHEO9 404(未找到)”。 I understand the reason: the true address must be http: // localhost: 3000 / socket.io /. 我了解原因:真实地址必须为http://本地主机:3000 / socket.io /。 Do you know how I can correct? 你知道我该如何纠正吗?

I have read many discussions, but no one has a suitable solution for version 2.1.0, even in a discussion I read a downgrade proposal, I would like to avoid it. 我已经阅读了很多讨论,但是没有人为2.1.0版提供合适的解决方案,即使在讨论中我阅读了降级建议,我也希望避免。

client.js: client.js:

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
  $(function () {
    var socket = io().connect('http://localhost:3000');
    $('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>

server.js: server.js:

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

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);
  });
});

The problem here is that you've defined any endpoint to server socket.io client file. 这里的问题是您已经定义了服务器socket.io客户端文件的任何端点。 That's why you are getting 404 . 这就是为什么您得到404的原因。

If you've installed in with npm , you can allow express to server that kind of static files for you by just adding the following line 如果您使用npm安装,则只需添加以下行即可允许向服务器表达这种静态文件

app.use(express.static(__dirname + '/node_modules'));  

if you've installed socket.io with bower, use 如果您已使用凉亭安装了socket.io ,请使用

 app.use(express.static(__dirname + '/node_modules'));  

Or you can place that socket.io 's client library at any location and server the file against the requests like 或者,您可以将该socket.io的客户端库放在任何位置,并根据诸如

app.get('/socket.io', function(req, res){
  res.sendFile("Path to socket.io.js file");
});

And you don't need to write full address in 而且您无需在其中写入完整的地址

<script src="http://localhost:3000/socket.io/socket.io.js"></script>

Just use 只需使用

<script src="/socket.io/socket.io.js"></script>

Change this: 更改此:

var socket = io().connect('http://localhost:3000');

to this: 对此:

var socket = io.connect('http://localhost:3000');

or to this: 或对此:

var socket = io('http://localhost:3000')

Either of these last two are the same. 最后两个都是相同的。


You will want to use only one form of connect, either io(...) or io.connect(...) , not both. 您将只想使用一种连接形式,即io(...)io.connect(...) ,不能同时使用。 When you call just io() you're trying to connect to the default URL which will be the URL of the current web page and then when you then try to also do .connect() after that, you're trying to connect again. 当您仅调用io()您尝试连接到默认URL,该默认URL将是当前网页的URL,然后又尝试在此之后也执行.connect() ,您尝试再次连接。 You only want one connection and to the specified URL. 您只需要一个连接并连接到指定的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM