socket.io - 不能让它工作,404有一些轮询呼叫 [英] socket.io - can't get it to work, having 404's on some kind of polling call

查看:514
本文介绍了socket.io - 不能让它工作,404有一些轮询呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

问题来自于socket.io,我不知道它是客户端还是服务器端。似乎正在尝试不断地轮询服务器,但正在回到404。这听起来像socket.io没有运行,但这一切看起来都对我好。它也可能与路径有关,并有一个公共目录,但我不知道。

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.

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"

服务器

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

客户

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

我从客户端获取了js文件:
node_modules / socket.io / node_modules / socket .io-client / socket.io.js

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

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

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.

在这里有任何帮助?

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

EDIT For 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

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

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.

推荐答案

看起来像Socket.IO不要以 /socket.io / 开头拦截请求。这是因为在你的情况下,监听器是 app - 一个Express处理程序。您必须使 http 被监听,以便Socket.IO可以访问请求处理。

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 http be listener, so that Socket.IO will have access to request handling.

尝试替换

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

http.listen(8080, "127.0.0.1");

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

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

这篇关于socket.io - 不能让它工作,404有一些轮询呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆