I'm trying to use the router and socket.io together.
I've created a separate router file and try to conjunct the route and 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)
.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.