贴一下 ./bin/www的关键代码
#!/usr/bin/env node
/**
* Faye pub sub server
*/
var bayeux = require('../app/services/faye_service').bayeux;
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('xxx:server');
var http = require('http');
app.set('port', 3104);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* attach Faye publish events to server
*/
bayeux.attach(server);
我的经验就是 把bayeux放在所有app.js内容之前,(20160119-10:50更新 放在server之后 attach之前 也可以)
然后controller里面的发布才会正常.
然后attach,然后再listen.
faye_service里面的关键代码:
var faye = require('faye');
var bayeux = new faye.NodeAdapter({mount: '/pubsubservice', timeout: 60});
module.exports = {
bayeux : bayeux,
publish : function(channel,infoObj){
console.log("in service publish");
this.bayeux.getClient().publish(channel, infoObj);
//console.log(this.bayeux);
console.log('out service publish');
}
};
controller里面的关键代码:
fayeService.publish('/saveok',{"info":"OK"});
var client = new Faye.Client('http://localhost:3104/pubsubservice');
var subscription = client.subscribe('/saveok', function(message) {
// handle message
console.log(message);
subscription.cancel();
});