Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

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

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

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

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"

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

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.

Any help here?

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

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.

question from:https://stackoverflow.com/questions/24793255/socket-io-cant-get-it-to-work-having-404s-on-some-kind-of-polling-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...