[英]Working with express.io in a separate router file
I'm trying to use the router and socket.io together. 我正在尝试一起使用router和socket.io。
I've created a separate router file and try to conjunct the route and socket.io 我创建了一个单独的路由器文件,并尝试将route和socket.io连接在一起
app = require('express.io')();
//var app = express();
//var router = express.Router();
app.http().io();
var mysql = require('mysql');
var connection = mysql.createConnection({
host :'aaaa',
port : 3306,
user : 'bbbb',
password : 'cccc',
database:'dddd'
});
connection.connect(function(err) {
if (err) {
console.error('mysql connection error');
console.error(err);
throw err;
}
});
/* GET home page. */
app.get('/', function(req, res) {
var query = connection.query('select * from xe_livexe_rss limit 0,1',function(err,rows){
console.log(rows);
//res.json(rows);
res.render('index', { title: 'Express',rss:rows });
req.io.route('ready');
});
});
app.io.route('ready', function(req,res) {
req.io.emit('talk', {
message: 'io event from an io route on the server'
});
});
module.exports = app;
but,,when I request the / router, it fails with the following message. 但是,当我请求/路由器时,它失败并显示以下消息。
TypeError: Object #<Object> has no method 'emit'
at Object.module.exports [as ready] (/home/ubuntu/nodetest1/routes/index.js:41:12)
at Manager.io.route (/home/ubuntu/nodetest1/node_modules/express.io/compiled/index.js:85:29)
at Object.request.io.route (/home/ubuntu/nodetest1/node_modules/express.io/compiled/index.js:197:29)
at Query._callback (/home/ubuntu/nodetest1/routes/index.js:34:10)
at Query.Sequence.end (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
at Query._handleFinalResultPacket (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
at Protocol._parsePacket (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/Protocol.js:213:24)
at Parser.write (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/Protocol.js:37:16)
Just wonder what I have missed... 只是想知道我错过了什么...
By your error message, I can say callback fucntion for ready
is not receiving a SocketRequest
object and it gives you access to the RequestIO
object which has an event your are trying to access emit(event, data)
. 根据您的错误消息,我可以说
ready
回调功能未接收到SocketRequest
对象,它使您可以访问RequestIO
对象,该对象具有您尝试访问的emit(event, data)
。
I guess you code has a bug as higlited below: 我猜你的代码有一个如下所示的错误:
/* GET home page. */
app.get('/', function(req, res) {
var query = connection.query('select * from xe_livexe_rss limit 0,1',function(err,rows){
console.log(rows);
//res.json(rows);
res.render('index', { title: 'Express',rss:rows });
req.io.route('ready'); // <==
//When this event is invoked there will be no req/res object passed to your `ready` event.
//This has to be invoked by client script. like
//Emit ready event.
/* `io.emit('ready')`; */ <== clientPage.html
});
});
I have the following code working: 我有下面的代码工作:
app = require('express.io')();
app.http().io();
// Setup the ready route, and emit talk event.
app.io.route('ready', function(req) {
req.io.emit('talk', {
message: 'io event from an io route on the server'
})
});
// Send the client html.
app.get('/', function(req, res) {
res.sendfile(__dirname + '/client.html')
});
app.listen(7076);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.