带有快速生成器的 socket.io
Posted
技术标签:
【中文标题】带有快速生成器的 socket.io【英文标题】:socket.io with express generator 【发布时间】:2016-09-04 00:10:02 【问题描述】:在写这篇文章之前,我尝试了Using socket.io in Express 4 and express-generator's /bin/www的解决方案
终端在/routes/messages.js:12
上显示错误
io.on('connection', function(socket)
TypeError: Cannot read property 'on' of undefined
我的文件来了:
/routes/messages.js
var express = require('express');
var controller = require('../controllers/messageController');
var passport = require('passport');
var router = express.Router();
router.get('/', controller.plain);
module.exports = function(io)
var router = express.Router();
io.on('connection', function(socket)
console.log('**********************************************************');
console.log('mensaje desde socket.io en el archivo de rutas messages.js');
console.log('**********************************************************');
);
return router;
;
/bin/www
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('archiers:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
// socket.io
var io = app.io;
io.attach(server);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val)
var port = parseInt(val, 10);
if (isNaN(port))
// named pipe
return val;
if (port >= 0)
// port number
return port;
return false;
/**
* Event listener for HTTP server "error" event.
*/
function onError(error)
if (error.syscall !== 'listen')
throw error;
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code)
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
/**
* Event listener for HTTP server "listening" event.
*/
function onListening()
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
/app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var session = require('express-session');
var socket = require('socket.io');
// var for routes
var index = require('./routes/index');
var users = require('./routes/users');
var projects = require('./routes/projects');
var messages = require('./routes/messages')(io);
var app = express();
// socket.io
var io = socket();
app.io = io;
io.on( "connection", function( socket )
console.log( "A user connected" );
);
// para devolver estado por consola.
app.use(morgan('combined'));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var Account = require('./models/accounts');
passport.use(new LocalStrategy(Account.authenticate()));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser());
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( extended: false ));
app.use(cookieParser());
//app.use(multer(dest : "./userFiles/projectImage"))
app.use(express.static(path.join(__dirname, 'public')));
app.use(require('express-session')(
secret: 'secretpass',
resave: true,
saveUninitialized: true,
cookie: maxAge : 3600000 //1 Hour
));
app.use(passport.initialize());
app.use(passport.session());
//Rutas express
app.use('/', index);
app.use('/user', users);
app.use('/project', projects);
app.use('/message', messages)
// catch 404 and forward to error handler
app.use(function(req, res, next)
var err = new Error('Not Found');
err.status = 404;
next(err);
);
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development')
app.use(function(err, req, res, next)
res.status(err.status || 500);
res.render('error',
message: err.message,
error: err
);
);
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next)
res.status(err.status || 500);
res.render('error',
message: err.message,
error:
);
);
mongoose.connect('mongodb://localhost/db');
module.exports = app;
【问题讨论】:
Marcos 当您真的想对@Gilad 发表评论时,您不必回答问题。对于您的问题,如果您从 app.js 中删除 io.on('connection') 并且只在 messages.js 中有 io.on('connection') 会发生什么? 我添加了 var messages = require('./routes/messages')(io);正如Gilad所说,这解决了错误,接下来是当有人连接到浏览器上的/message时查看日志消息。 好的!所以试试我说的话,也许你 app.js 中的第二个 io.on('connection') 会覆盖你在 messages.js 文件中设置的第一个。从你的 app.js 中删除代码 io.on('connection', ...) 终端没有发生任何事情:************************************* ***************************** aquí vendran los mensajes del socket.io desde messageController.js ********* ****************************************************** ******* ::ffff:127.0.0.1 - - [09/May/2016:14:15:34 +0000] "GET /message HTTP/1.1" 500 2744 "-" "Mozilla/5.0 (X11 ; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0" GET /message 500 165.845 ms - 2744 【参考方案1】:在 app.js 你有这个代码:
var messages = require('./routes/messages')(io);
var app = express();
// socket.io
var io = socket();
app.io = io;
您应该在将 io 变量传递给消息导出函数之前对其进行初始化 - 如下所示:
var app = express();
// socket.io
var io = socket();
app.io = io;
var messages = require('./routes/messages')(io);
【讨论】:
以上是关于带有快速生成器的 socket.io的主要内容,如果未能解决你的问题,请参考以下文章
用nodejs快速实现websocket服务端(带SSL证书生成)
使用 node express 生成器配置 socket.io
带有快速会话的 ReactJS 和 socket.io - 套接字未使用正确的会话并创建了许多其他会话