node.js socket.io - 无法让它工作,在某种轮询调用中有 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24793255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:39:52  来源:igfitidea点击:

socket.io - can't get it to work, having 404's on some kind of polling call

node.jsexpresssocket.iopug

提问by Eli

I'm trying to get a server set up with socket.io, with bad results.

我正在尝试使用 socket.io 设置服务器,但结果不佳。

I am mostly following this guide, although it's somewhat out of date: http://www.williammora.com/2013/03/nodejs-tutorial-building-chatroom-with.html

我主要遵循本指南,尽管它有些过时:http: //www.williammora.com/2013/03/nodejs-tutorial-building-chatroom-with.html

The problem comes with socket.io, I'm not sure if it's client or server side. It appears to be trying to continuously poll the server, but is getting 404's back. That sounds like socket.io isn't running, but it all looks okay to me. It may also have something to do with paths and having a "public" directory, but I don't really know.

问题来自socket.io,我不确定它是客户端还是服务器端。它似乎正在尝试不断轮询服务器,但正在返回 404。这听起来像是 socket.io 没有运行,但在我看来一切正常。它也可能与路径和“公共”目录有关,但我真的不知道。

127.0.0.1 - - [Thu, 17 Jul 2014 00:51:36 GMT] "GET /socket.io/?EIO=2&transport=polling&t=1405558296120-0 HTTP/1.1" 404 73 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML, like Gecko) Version/7.0.4 Safari/537.76.4"
127.0.0.1 - - [Thu, 17 Jul 2014 00:51:37 GMT] "GET /socket.io/?EIO=2&transport=polling&t=1405558297181-1 HTTP/1.1" 404 73 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML, like Gecko) Version/7.0.4 Safari/537.76.4"
127.0.0.1 - - [Thu, 17 Jul 2014 00:51:39 GMT] "GET /socket.io/?EIO=2&transport=polling&t=1405558299207-2 HTTP/1.1" 404 73 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML, like Gecko) Version/7.0.4 Safari/537.76.4"

Server

服务器

var exec = require( "child_process" ).exec;
var path = require( "path" );
var morgan = require( "morgan" );
var bodyParser = require( "body-parser" );
var _ = require( "underscore" );
var express = require( "express" );
var app = express();
var http = require( "http" ).createServer( app );
var io = require( "socket.io" )( http );

app.set( "ipaddr", "127.0.0.1" );
app.set( "port", 8080 );

//support JSON, urlencoded, and multipart requests
app.use( bodyParser() );

//log the requests using morgan
app.use( morgan() );

//specify the Jade views folder
app.set( "views", __dirname + "/../views" );

//set the view engine to Jade
app.set( "view engine", "jade" );

//specify static content
app.use( express[ "static" ]( "public", __dirname + "/../public" ) ); //using map-access of static so jslint won't bitch

io.on( "connection", function( socket )
{
    console.log( "A user connected." );
});

Client

客户

script( src="js/socket.io.js" )
var socket = io.connect( "http://localhost:8080" );

I got the client js file from: node_modules/socket.io/node_modules/socket.io-client/socket.io.js

我从以下位置获得了客户端 js 文件:node_modules/socket.io/node_modules/socket.io-client/socket.io.js

That path doesn't match either what the tutorial I'm using says, or what socket.io says to use, so maybe that's the problem, but it looks like it's probably the correct file.

该路径与我正在使用的教程所说的或 socket.io 所说的不匹配,所以也许这就是问题所在,但它看起来可能是正确的文件。

Any help here?

这里有什么帮助吗?

EDITFor additional information, here is my folder hierarchy:

编辑有关其他信息,这是我的文件夹层次结构:

Webserver/
    my_modules/
        server.js
    node_modules/
        body-parser/
        express/
        jade/
        morgan/
        socket.io/
        underscore/
    public/
        css/
        js/
            server.js
    views/
        index.jade
    index.js
    package.json

The server.js in the my_modules folder is where I start socket.io on the server side. The server.js in the js folder is the client code to connect from the website.

my_modules 文件夹中的 server.js 是我在服务器端启动 socket.io 的地方。js 文件夹中的 server.js 是从网站连接的客户端代码。

回答by Oleg

It looks like Socket.IO can't intercept requests starting with /socket.io/. This is because in your case the listener is app-- an Express handler. You have to make httpbe listener, so that Socket.IO will have access to request handling.

看起来 Socket.IO 无法拦截以/socket.io/. 这是因为在您的情况下,侦听器是app——一个 Express 处理程序。您必须http成为侦听器,以便 Socket.IO 可以访问请求处理。

Try to replace

尝试更换

app.set( "ipaddr", "127.0.0.1" );
app.set( "port", 8080 );

with

http.listen(8080, "127.0.0.1");

See docs for details: http://socket.io/docs/#using-with-express-3/4

有关详细信息,请参阅文档:http: //socket.io/docs/#using-with-express-3/4

回答by Camilo

In my case, I created my app with the Express application generator. To solve this problem, instead of edit the app.js file on the root folder of the project, I edited the file bin/www on the line after the server is defined:

就我而言,我使用Express 应用程序生成器创建了我的应用程序。为了解决这个问题,我没有在项目的根文件夹编辑app.js文件,而是在定义服务器后在一行编辑了文件bin/www:

/**
 * Create HTTP server.
 */

var server = http.createServer(app);
var io = require('socket.io')(server); // Add this here

Update

更新

I found a better way here Express Generator and Socket.io

我在这里找到了更好的方法Express Generator 和 Socket.io

回答by lgargantini

Just for check that is your server or just client problem you could use this web: websocket-echo, if this client connect right to your server (the first form client is usefull if your server is online, if is on your host, cuoting from websocket.org...

只是为了检查是您的服务器还是只是客户端问题,您可以使用此 Web:websocket-echo,如果此客户端正确连接到您的服务器(如果您的服务器在线,则第一种形式的客户端很有用,如果在您的主机上,则从websocket.org...

Using a text editor, copy the following code and save it as websocket.html somewhere on your hard drive

使用文本编辑器,复制以下代码并将其保存为 websocket.html 硬盘上的某个位置

Code: websocket-test

代码:websocket-test

The only thing different from mine that i could observe is the io client source: <script src="/socket.io/socket.io.js"></script>on your client side.

我可以观察到的与我的唯一不同的是 io 客户端源: <script src="/socket.io/socket.io.js"></script>在您的客户端。

On your server you should try this way:

在您的服务器上,您应该尝试这种方式:

var express = require('express'), 
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
exec = require('child_process').exec,
util = require('util');

//serve our code
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded());
//listening on connections

io.on('connection', function (socket) {
    console.log('client connected!'); 
}
server.listen(8080);

Note that this way works fine with this dependencies: I recommend you add this code below to your package.json file:

请注意,这种方式可以很好地处理此依赖项:我建议您将此代码添加到您的 package.json 文件中:

"dependencies": {
    "express": "3.x.x",
    "socket.io": "*",
    "util": "*"
  }

cheers!

干杯!