node.js - Express Socket.io example server not working -
i'm trying learn socket.io , example express isnt working. heres code:
app.js: var express = require('express') , routes = require('./routes') , user = require('./routes/user') , server = require('http').createserver(app) , path = require('path') , io = require('socket.io').listen(app); var app = express(); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(app.router); app.use(require('less-middleware')({ src: __dirname + '/public' })); app.use(express.static(path.join(__dirname, 'public'))); server.listen(3000); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.jade'); }); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
and index.jade file:
extends layout block content script(src="/node_modules/socket.io/lib/socket.io.js") script( var socket = io.connect('http://localhost:3000'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); )
when go localhost browser keeps waiting on response. times out.
most of other questions related question had odd implementation of example code. i'm literally trying implement show.
the problem creating app
after creating io
. line
, io = require('socket.io').listen(app);
is same as
, io = require('socket.io').listen(undefined);
remove line
var app = express();
and somewhere between first line , io = require('socket.io')
add , app = express()
Comments
Post a Comment