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
685 views
in Technique[技术] by (71.8m points)

node.js - GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)

I'm trying to stream data to the browser. I'm struggling, however, to connect it to the browser. Here's my html:

<ul class="tweets"></ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect();
    jQuery(function ($) {
        var tweetList = $('ul.tweets');
        socket.on('tweet', function (data) {
            tweetList .prepend('<li>' + data.user + ': ' + data.text + '</li>'); 
        }); 
    });
</script>

And here's the relevant parts of my app.js:

var express = require('express')
    , twitter = require('ntwitter')
  , http = require('http')
  , path = require('path');

var app = express();

var io = require('socket.io').listen(app);

app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); });

app.listen(app.get('port'), function(){
   console.log("Express server listening on port " + app.get('port'));
});

io.sockets.volatile.emit('tweets', {
            user: data.user.screen_name,
            text: data.text,
            geo : geo,
            latitude: latitude,
            longitude: longitude
                });

I installed socket.io 0.9.16 via my packages.json file:

"dependencies": {
    "express": "3.2.6",
    "jade": "*",
    "ntwitter":"0.2.10",
    "socket.io":"0.9.x"
  }

Can anyone help me out here? Why can't it find the file?

Digging a bit deeper. To test the socket, I put this in the app.js:

var socket = io.listen(app);

And I get the error:

TypeError: Object #<Manager> has no method 'listen'
    at Object.<anonymous> (/home/andy/dev/node/mytwittermap/app.js:49:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Your setup needs to look something like this:

var app     = express();
var server  = require('http').createServer(app);
var io      = require('socket.io').listen(server);
...
server.listen(app.get('port')); // not 'app.listen'!

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

...