好的,所以我正在尝试学习 socket.io,但我收到了这个错误:
io.socket.on('connection', function (socket) TypeError: Cannot call method 'on' of undefined
这是我的代码:
var express = require('express')
, app = express.createServer()
, routes = require('./routes')
, user = require('./routes/user')
, path = require('path')
, io = require('socket.io').listen(app);
// all environments
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(express.static(path.join(__dirname, 'public')));
app.listen(3000);
app.get('/', routes.index);
app.get('/users', user.list);
io.socket.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
我知道这将是相当明显的事情。